I would like to have some lambda functions available to all instances of one class. Therefore, my idea was to declare the lambda function as class attribute.
In the following simplistic code, why can't I evaluate the following lambda function f
that I have defined as a class attribute?
In [1]: class MyClass():
...: f = lambda x : 2 * x + 1
...: def __init__(self):
...: pass
In [2]: Inst = MyClass()
In [3]: MyClass.f
Out[3]: <unbound method MyClass.<lambda>>
In [4]: MyClass.f(2)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-4-5fc154bfb75c> in <module>()
----> 1 MyClass.f(2)
TypeError: unbound method <lambda>() must be called with MyClass instance as first argument (got int instance instead)
In [5]: Inst.f(3)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-5-90cde1a87da4> in <module>()
----> 1 Inst.f(3)
TypeError: <lambda>() takes exactly 1 argument (2 given)