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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With