Comments (4)
A couple corrections:
- Python 2.7 doesn't have an
Enum
(you must be using a third-party library, possibly myenum34
) is_aaa
is not a class method
Thanks for the report, but this is expected behavior, and will also fail in 3.11+.
History:
In Python 3.4, when Enum was created, it was not possible to reference one member from another (so F.AAA.BBB
failed, as did self.AAA
). In 3.5, due to a speed boost implementation detail, it became possible, but was strongly discouraged.
Future:
In Python 3.11 it is again possible to disallow member-to-member access, so that is what is happening. Note that member-to-class-to-member access works fine:
@property
def is_aaa(self):
return self is self.__class__.AAA
On a stylistic note, the recommended way to test for an enum member is
some_var = F.BBB
if some_var is F.AAA:
# stuff
from aenum.
Thanks @ethanfurman
Yes, enum34 on Python2.7, and yes I prefer to use aenum (as I am using Flags
too).
Yes, it didn't feel right accessing other defined enum values via self
, but that is allowable with standard classes.
Using self.__class__.AAA
works, as does E.AAA
. Is one more efficient than the other?
I felt encapsulating the test in a method made the code more robust as I have the test in multiple places, so less chance of getting the test wrong somewhere (e.g. in a refactor). If you had to test for multiple things then that probability increases.
e.g if my_enum in [ X.CCC, X.AAA, X.FFF, X.EEE ] : # do stuff
Having that test repeated in many places is more error prone. One option is to set a module level variable with the list of things to check.
CAFE = [ X.CCC, X.AAA, X.FFF, X.EEE ]
if my_enum in CAFE:
# do stuff
Not sure what the python purists prefer, but I like the encapsulating in a function approach - but as always can be persuaded ;-)
Cheers :)
from aenum.
E.AAA
is only two lookups, while self.__class__.AAA
is three.
If testing multiple things at once a function is fine. I made the comment mainly because you were comparing with ==
instead of is
from aenum.
Yep. Using timeit
I can see that is
performs much better than ==
.
Presumably it's a simple pointer comparison rather than a call to an equivalence function that does some checking.
Thanks :)
from aenum.
Related Issues (20)
- Convert test script to proper unittest test module HOT 1
- Regression - extend_enum broken for Flag and IntFlag as of version 3.0.0 HOT 2
- Regression - extend_enum broken on 3.1.1 using py36 HOT 1
- Why does `_generate_next_value_` method of `StrEnum` returns lower case? HOT 1
- [BUG] RecursionError when using aenum along with with custom enum member access hook HOT 4
- extend an IntEnum with documentation fails
- add Licence HOT 1
- Pylint complains unsubscriptable-object when using AutoNumberEnum
- Allow creating NoAlias enums from enum members
- successive auto()'s not functioning correctly with _init_ HOT 1
- Tag releases HOT 6
- [question] How would one make value comparisons between separate enums possible? HOT 1
- Test failure with Python 3.11
- aenum 3.1.12 is on PyPi but not GitHub HOT 3
- mypy error when trying to iterate over aenum.Enum subclass HOT 1
- aenum 3.1.13 fails to import if sqlite3 does not exist HOT 1
- Package is broken in 3.1.14 ModuleNotFoundError: No module named 'aenum._common' HOT 4
- `aenum>=3.1.13` fails to import on PyPy3
- Not throwing TypeError if `extend_enum` is called without the required number of arguments
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from aenum.