Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix Python Enum - AttributeError(name) from None error?

Trying to use an enum in Python 3.7.3, getting the following error. Already tried to install - and uninstall - enum34, but it still does not work. Did all the operations in a virtual environment (as the error shows).

Is there anything else I can do to fix this (except using another enum implementation as shown in this question)?

#enum import:
from enum import Enum

# enum definition:
class Status(Enum):
    on: 1
    off: 2

# enum utilisation (another class, same file):
self.status = Status.off

# error:
File "C:\dev\python\test\venv\lib\enum.py", line 349, in __getattr__
AttributeError(name) from None
AttributeError: off
like image 352
evilmandarine Avatar asked Jun 10 '19 19:06

evilmandarine


2 Answers

The correct syntax for defining an enum is:

class Status(Enum):
    on = 1
    off = 2

Not on: 1.

like image 132
jwodder Avatar answered Sep 21 '22 05:09

jwodder


In your definition, use = to assign values to the attributes, not :.

# enum definition:
class Status(Enum):
    on = 1
    off = 2
like image 25
c0x6a Avatar answered Sep 21 '22 05:09

c0x6a