if coin == 25 | 10 | 5:
If I replace the ‘|’ with ‘or’ the code runs just fine. I’m not sure why I can’t use ‘|’ in the same statement.
Doing the following doesn’t work either:
if coin == 25 | coin == 10 | coin == 5:
I know bitwise operators can only be used with integers, but other then that is there another difference from logical operators?
while many great things have been said in this thread i’ll also link to the docs for
enum.Flag
(docs.python.org) which wraps bitmap integer enumsWow that’s neat! Thanks for bringing this up.
The feature flags example should be rewritten to use
enum.Flag
one small gripe i have is that the repr¹ doesn’t handle aliases (ie items with more than one bit set) that well , but tbh its not an easy problem
example to illustrate the problem :
class Foo(enum.Flag): A = auto() B = auto() C = auto() # no D AB = 0b0011 CD = 0b1100 print(Foo.AB | Foo.C) # <Foo.A|B|C: 7> NOT <Foo.AB|C: 7> print(Foo.CD | Foo.A) # <Foo.A|C|CD: 13> NOT <Foo.A|CD: 7>
its a minor thing but it annoys me a bit
[1]: the
_name_
member , which is used by__repr__
, of an enum member is actually generated either when the individual enum class is created or when the value is first needed by_missing_
. also the docs call these names surrounded by single underscores “sunder” names