function as attr of subclass != method (???)

Adrian Eyre a.eyre at optichrome.com
Thu Nov 25 07:50:02 EST 1999


> >>> achild.method = lambda self:"nope"
> >>> achild.method()
> Traceback (innermost last):
>   File "<pyshell#102>", line 1, in ?
>     achild.method()
> TypeError: not enough arguments; expected 1, got 0

achild is an instance. You must assign the lambda as a member
of the child *class*, else it will be an unbound method.

e.g.

>>> class A:
...     pass
...
>>> b = lambda x: x
>>> b
<function <lambda> at 7f7bd0>
>>> a.b = b
>>> a.b
<function <lambda> at 7f7bd0>
>>> a.b()
Traceback (innermost last):
  File "<stdin>", line 1, in ?
TypeError: not enough arguments; expected 1, got 0
>>> a.b(1)
1
>>> A.b = b
>>> a=A()
>>> A.b
<unbound method A.<lambda>>
>>> a.b
<method A.<lambda> of A instance at 7f8d70>
>>> a.b()
<__main__.A instance at 7f8d70>

--------------------------------------------
Adrian Eyre <mailto:a.eyre at optichrome.com>
Optichrome Computer Solutions Ltd
Maybury Road, Woking, Surrey, GU21 5HX, UK
Tel: +44 1483 740 233  Fax: +44 1483 760 644
http://www.optichrome.com 
--------------------------------------------





More information about the Python-list mailing list