I just want to achieve some extra which is not there in operator module of python like not_contain. For that I created my own class with the not_contain method. I just want to do some more variation and for that I need the class. I am using python3.
class UserDefinedOperators:
def not_contain(self, a, b):
return a not in b
def some_other_function(self, a, b):
some other work here
Now I am creating an Enum like this
import operator
import enum
class MyOperators(enum.Enum):
LT = operator.gt
GT = operator.gt
NOT_CONTAIN = UserDefinedOperators().not_contain
I am having following differences in accessing the Enum value
>>> MyOperators
<enum 'MyOperators'>
>>> MyOperators.LT
<MyOperators.LT: <built-in function gt>>
>>> MyOperators.LT.value
<built-in function gt>
>>> MyOperators.LT.name
'LT'
>>> MyOperators.NOT_CONTAIN
<bound method UserDefinedOperators.not_contain of <__main__.UserDefinedOperators object at 0x100cdbeb0>>
>>> MyOperators.NOT_CONTAIN.value
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'function' object has no attribute 'value'
>>> MyOperators.NOT_CONTAIN.name
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'function' object has no attribute 'name'
>>>
So with the class function, the Enum shows
AttributeError: 'function' object has no attribute 'value'
I read on other SO answer that it is getting assigned as function and not Enum attributes that is why it doesn't have .value or .name attributes. But then built in operator.lt and operator.gt is also a function then how come it is working fine and have .value and .name properties?
The error does come with normal function not just class function but I want to understand what difference it is in operator module? How can I achieve the same with my class?