Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How are lambda expressions bound to a class?

Tags:

python

If I set up a class like below in Python, as I expect the lambda expressions created should be bound to the class A. I don't understand why when I put a lambda inside a 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()

#a.f bound
print a.f
<bound method A.<lambda> of <__main__.A object at 0xb743350c>>

#a.g[0] not bound
print a.g[0]
<function <lambda> at 0xb742d294>

Why is one bound and not the other?

like image 599
Mike Avatar asked Jun 30 '11 04:06

Mike


1 Answers

f is bound because it's a part of the class as per the definition. g is not a method. g is a list. The first element of this list incidentally happens to be a lambda expression. That's got nothing to do with whether g is defined inside a class definition or not.

like image 148
Noufal Ibrahim Avatar answered Sep 29 '22 10:09

Noufal Ibrahim