I'm trying to assign class methods to class attribute, so I can call the methods from string. When using the class I want to call it from string like:
A.MAP['add'](x, y)
A.MAP['subtract'](x, y)
This is my current code:
class A:
MAP = {
'add' : A.add(x, y),
'subtract' : A.subtract(x, y),
}
@classmethod
def add(cls, x, y)
return x + y
@classmethod
def subtract(cls, x, y)
return x - y
However the result shown error that A is not defined
at the line of assigning A.add
to MAP['add']
. For short functions I can use lambda
. However, in case of a longer function, how can I achieve this design?