this post was submitted on 09 Mar 2025
18 points (87.5% liked)

Python

6788 readers
42 users here now

Welcome to the Python community on the programming.dev Lemmy instance!

📅 Events

PastNovember 2023

October 2023

July 2023

August 2023

September 2023

🐍 Python project:
💓 Python Community:
✨ Python Ecosystem:
🌌 Fediverse
Communities
Projects
Feeds

founded 2 years ago
MODERATORS
 

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?

you are viewing a single comment's thread
view the rest of the comments
[–] MinekPo1@lemmygrad.ml 3 points 21 hours ago (1 children)

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 enums

[–] logging_strict@programming.dev 2 points 11 hours ago (1 children)

Wow that's neat! Thanks for bringing this up.

The feature flags example should be rewritten to use enum.Flag

[–] MinekPo1@lemmygrad.ml 1 points 8 hours ago

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