How are lambda expressions related to the class?

If I set up the class as shown below in Python, since I expect the created lambda expressions to be bound to class A. I don’t understand why, when I put the lambda inside the list, like in g , it isn’t bound .

 class A(object): f = lambda x,y: (x + y) g = [lambda x,y: (x + y)] a = A() #af bound print af <bound method A.<lambda> of <__main__.A object at 0xb743350c>> #ag[0] not bound print ag[0] <function <lambda> at 0xb742d294> 

Why one binding and not another?

+7
source share
2 answers

f bound because it is part of the class as defined. g not a method. g is a list. The first element of this list, by the way, is a lambda expression. This has nothing to do with the definition of g inside the class definition or not.

+15
source

If you want g[0] also be a related method, follow these steps:

 class A(object): f = lambda x,y: (x + y) _ = lambda x,y: (x + y) g = [_] 
0
source

All Articles