#πŸ”’ Enum interseting behaviour? I want to know why?

21 messages Β· Page 1 of 1 (latest)

foggy oriole
#
from enum import StrEnum, auto
class Foo(StrEnum):
  A = auto()
  B = auto()
  C = auto()

# ValueError why? and why it looks like im making an instance and giving arguments but it gives back an instance of existing values only
Foo('A')

# This works
Foo('a')

# I figured out that auto when using StrEnum it uses alphabetical charecters

# Key error, i figured out that doing like that its accesing the members dictionary, so it makes sense to me
Foo['a']

# This works and ill be using that
Foo['A']

You might ask why im doing like that cuz Enums ussually are used Foo.A but im loading things from a file so i have a string value

main cosmosBOT
#

@foggy oriole

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

real silo
#

It wants equality to a value, and the auto-values here are a, b, c. You're probably looking for Foo["A"] or something like that.

foggy oriole
trail inlet
# foggy oriole ```py from enum import StrEnum, auto class Foo(StrEnum): A = auto() B = auto...

!e
An enum item has two things: the "name" and the "value". For example:

from enum import Enum

class ColorChannel(Enum):
    RED = 1
    GREEN = 2
    BLUE = 69

# members are looked up by their "name"
print(ColorChannel.RED, ColorChannel.GREEN, ColorChannel.BLUE)
print(list(ColorChannel))

# but calling the enum class looks up items by value
print(ColorChannel(2))
# print(ColorChannel(3)) -- error
main cosmosBOT
trail inlet
#

So essentially you're doing ```py
class Foo(StrEnum):
A = "a"
B = "b"
C = "c"

foggy oriole
#

well yea, but i havent found in the docs where it says that, i only found that i can access the members by key in the __getitem__ method

trail inlet
#

where it says what?

foggy oriole
#

that it checks for value when doing Test(2)

#

shit im blind πŸ˜„

trail inlet
trail inlet
#

using __call__ for two completely different purposes is also cursed

#

(I'm looking at you type)

foggy oriole
#

Anyway thanks for help, now i know πŸ˜„

#

!close

main cosmosBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.