6

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)
MarcoMag
  • 546
  • 6
  • 17
  • Interestingly, this code works just fine in 3.X. I wonder what changed between versions? – Kevin Mar 03 '17 at 16:21
  • FWIW, lambda is for creating anonymous functions; binding a lambda to a name is generally considered to be bad style. – PM 2Ring Mar 03 '17 at 16:22

1 Answers1

10

It's as if you wrote the following:

class MyClass():
    def f(x):
        return 2 * x + 1

    def __init__(self):
        pass

The first parameter is named self by convention, so even if you didn't name it self, your function is an instance method whose first parameter is the current instance of MyClass.

You need to make your function a static method instead:

In [1]: %paste
    class MyClass():
        f = staticmethod(lambda x: 2 * x + 1)

        def __init__(self):
            pass

## -- End pasted text --

In [2]: MyClass.f(2)
Out[2]: 5
Blender
  • 275,078
  • 51
  • 420
  • 480