开发者

How are lambda expressions bound to a class?

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 want g[0] to be a bound method too, do:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜