assigned lambda as method: self not passed?

Question:

I want to create a method in a class object (depending on some condition). This works when I create the lambda in the class (m2) and when I assign an existing method to a class attribute (m3), but NOT when I assign a lambda to a class attribute (m1). In that case the lambda does not get the self parameter.

class c:

    def __init__( self ):
         self.m1 = lambda self: 1
         self.m3 = self._m3

    m2 = lambda self: 1
    
    def _m3( self ):
       pass

c().m2() # works
c().m3() # works
c().m1() # error: missing 1 required positional argument: 'self'

What must I do to be able to call the m1 lambda with the c().m1() syntax?

Asked By: Wouter van Ooijen

||

Answers:

Replace

self.m1 = lambda self: 1

by

self.m1 = lambda: 1
Answered By: Jasper Rou

A function needs to be bound to an instance for it to become a bound method of the instance and be passed with the instance as the first argument when called.

You can bind an unbound function to an instance with types.MethodType:

from types import MethodType

class c:
    def __init__(self):
        self.m1 = MethodType(lambda self: 1, self)
Answered By: blhsing