Reputation: 9804
How can I access an enum from another class - eg for class B as below:
from examples import A
class B:
properties = A
def __init__(self, name, area, properties):
self.name = name
self.area = area
self.properties = properties
B.property = B("test", 142.43, A)
print ("B color: "+B.properties.color)
print ("A color: "+str(A.color._value_))
#in separate module
from enum import Enum
class A(Enum):
color = "Red"
opacity = 0.5
print("A color: "+str(A.color._value_))
When I run class A :
A color: Red
When I run class B:
print ("B color: "+B.properties.color)
AttributeError: 'module' object has no attribute 'color'
Upvotes: 1
Views: 937
Reputation: 1121196
A
is the module containing your class, not the class itself. You'd have to reference the class in the module still:
from examples.A import A
or use
properties = A.A
and
print ("A color: "+str(A.A.color._value_))
Try to avoid using uppercase names for modules; the Python style guide (PEP 8) recommends you use all lowercase for module names. That way you won't so easily mix up modules and the classes contained in them.
Upvotes: 4