Giter Site home page Giter Site logo

Comments (4)

ethanfurman avatar ethanfurman commented on August 30, 2024

A couple corrections:

  • Python 2.7 doesn't have an Enum (you must be using a third-party library, possibly my enum34)
  • 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.

BrendanSimon avatar BrendanSimon commented on August 30, 2024

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.

ethanfurman avatar ethanfurman commented on August 30, 2024

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.

BrendanSimon avatar BrendanSimon commented on August 30, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.