-3

The following doesn't work in Python:

class MemorySize(int, Enum):
    "1024" = 1024
    "2048" = 2048

So what would be the closest way to do this without having to type the entire number out in words and make it type safe?

basickarl
  • 37,187
  • 64
  • 214
  • 335

2 Answers2

5

From docs:

An enumeration is a set of symbolic names (members) bound to unique, constant values. Within an enumeration, the members can be compared by identity, and the enumeration itself can be iterated over.

So, enum members should be symbolic names, not string literals.

If you want to use numbers as names you can prefix it with _ or m_, because private attributes start with underscore

Also, you can use IntEnum for integer enums.

from enum import IntEnum


class MemorySize(IntEnum):
    m_1024 = 1024
    m_2048 = 2048

print(MemorySize.m_1024.value)

Output

1024

Also, you can omit comma here "1024" = 1024,.

Actually 1024, is a tuple with one element (1024, ) and 1024 is just int. I was just curious, why you can pass tuple as well as int to Enum attribute.

I found out that the value of IntEnum attribute passes to int constructor. Before that it transforms args into tuple in EnumMeta.__new__

if not isinstance(value, tuple):
    args = (value, )
else:
    args = value

You can pass to int constructor second argument, the base: because int("ff", 16) == 255. Or just use string constant instead of int because int("123") == 123.

So, you can use IntEnum values with any number system like this (Although I don't think it's good way to use it in practice)

class WeiredEnum(IntEnum):
    m_255 = "ff", 16
    m_256 = "256"

print(WeiredEnum.m_255.value)
print(type(WeiredEnum.m_256.value))
> 255
> <class 'int'>
Yevhen Bondar
  • 4,357
  • 1
  • 11
  • 31
  • 1
    please provide explanation why your solution with _underscore_ works. – raiyan22 Jan 07 '22 at 14:23
  • 2
    @raiyan22 Because `_1024` is [valid python identifier](https://www.tutorialspoint.com/what-are-python-identifiers) – Yevhen Bondar Jan 07 '22 at 14:24
  • While this code may answer the question, it would be better to include some context, explaining _how_ it works and _when_ to use it *in the answer*. Code-only answers are not useful in the long run. – martineau Jan 07 '22 at 14:48
  • Must of use the ".value" at the end? – basickarl Jan 07 '22 at 14:51
  • 1
    @MrFuppes: As a [famous transcendentalist](https://en.wikipedia.org/wiki/Self-Reliance) once wrote: _"A Foolish Consistency is the Hobgoblin of Little Minds"_ (which PEP 8 [repeats](https://www.python.org/dev/peps/pep-0008/#a-foolish-consistency-is-the-hobgoblin-of-little-minds) and goes on to say "However, know when to be inconsistent…"). – martineau Jan 08 '22 at 02:18
  • @martineau yeah that was kind of stupid, hope it didn't cause you physical pain. Guess I was a bit perplexed by the question, why one should use a number to represent that number in an enum. – FObersteiner Jan 08 '22 at 10:54
  • 1
    @MrFuppes: Hard to say what the OP is planning on doing because they haven't revealed why or how they want their approach to "work", only that it "doesn't". – martineau Jan 08 '22 at 12:11
0

Don't do that!

Analyzing it, I realized that in this case it actually doesn't make sense to have an enum. If you have an enum of an integer that receives integer it is better not to have the enum at all and work only with the integer values, defining only the typing to limit it.

You can use the Literal type that was introduced in PEP 586 as a way to indicate that a variable must have a set of certain values.

from typing import Literal
ex: StatusCode = Literal[404, 200, 500]
Mithsew
  • 1,129
  • 8
  • 20