In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you what to pay attention to the use of Enum in Python, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
Enum is a class
So
Basic class operations can be used
That is, we can add our own methods.
Class Mood (Enum): FUNKY = 1 HAPPY = 3 def describe (self): # self is the member here return self.name, self.value def _ _ str__ (self): return'my custom str! {0} '.format (self.value) @ classmethod def favorite_mood (cls): # cls here is the enumeration return cls.HAPPY > > Mood.favorite_mood () > > Mood.HAPPY.describe () (' HAPPY') 3) > str (Mood.FUNKY)'my custom str! 1'
Each class member of Enum is automatically converted to an instance of the current class
From enum import Enumclass Color (Enum): RED = 1 GREEN = 2 BLUE = 3 > > type (Color.RED) > isinstance (Color.GREEN, Color) True
This means that we cannot use an enumerated member directly as its value:
> Color.RED = = 1False > Color.RED.value = = 1False
The enumeration member also has a name property, which is the same as its variable name
> Color.RED.name = = "RED" True
Enumerated classes have a big hole: when the parent class has members, it cannot define subclasses.
So, for the Color class above, if you want to define another subclass, you will make an error:
Class MoreColor (Color):... PINK = 17...Traceback (most recent call last):... TypeError: MoreColor: cannot extend enumeration 'Color'
However, the parent class does not have enumerated members, and it is possible to just define the function:
Class Foo (Enum): def some_behavior (self): passclass Bar (Foo): HAPPY = 1 SAD = 2
This obviously greatly limits the extension of the enumeration class, and a function that cannot access members is of little use.
A summary of Python Enum usage is attached
1. Enumerated classes cannot be used to instantiate objects
two。 To access an item in an enumerated class, you can access it directly using the class name plus the item to be accessed, such as color.RED
3. Key = Value defined in the enumeration class. The Value value cannot be modified outside the class.
4. Enumerated items can be used to compare, using = =, or is
5. The Key in the enumeration class cannot be the same, and the Value can be the same, but all Key with the same Value will be treated as aliases.
6. Enumerated classes can be traversed with for, and members.items () can traverse classes with aliases
7. If the key in the enumerated class cannot be the same, you need to import unique to decorate the enumerated class
The above is all the contents of the article "what are the precautions for the use of Enum in Python". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.