今天就学习课程吧!
今天就开通帐号,24,600 门业界名师课程任您挑!
Defining enumerations
- [Instructor] Python supports enumerations just like other popular programming languages and they're useful in a variety of scenarios. Usually, they are used to assign easy-to-read names to constant values in a program, which increases the readability of your code. They can also be used as hash values, and you can iterate over them like you would other iterables in Python. Enumerations are defined using the class syntax, so let's open up our sample code. I'm going to open up enums_start here in my Classes folder, and I'm going to define an enum by first importing the enum class from the enum package. So I'll write from enum import Enum then to define an enum, I'll create a new class and use the enum class as the superclass. So I'll write class and I'll use some fruits as my enumeration examples, and Enum will be my superclass. All right, so let's assign some values to our enum, and for this example I'll just use some fruit names. So I'll have APPLE equals one, BANANA is two, and…
随堂练习,边学边练
下载课堂讲义。学练结合,紧跟进度,轻松巩固知识。