Dynamic class methods misunderstanding

Hans Nowak hans at zephyrfalcon.org
Fri Jan 28 17:59:50 CET 2005


Bill Mill wrote:

> On Fri, 28 Jan 2005 11:09:16 -0500, Hans Nowak <hans at zephyrfalcon.org> wrote:
> <snip>
 >
>>To add m as a new method to the *class*, do this:
>>
>> >>> class test:
>>...     def __init__(self, method):
>>...         self.__class__.method = method
>>...         self.method()
>>...
>> >>> def m(self): print self
>>...
>> >>> test(m)
>><__main__.test instance at 0x0192ED78>
>><__main__.test instance at 0x0192ED78>
> 
> 
> When I run it, I only get one call to m, which is how I would expect
> python to work; I assume the double printing here is a typo?

Actually, no.  I'm using the interactive interpreter, so doing test(m) 
results in two lines: the first one is printed by m, the second one is 
the __repr__ of the test instance that was created, displayed by the 
interpreter.  Compare:

 >>> x = test(m)
<__main__.test instance at 0x0192ED78>
 >>> x
<__main__.test instance at 0x0192ED78>

-- 
Hans Nowak
http://zephyrfalcon.org/




More information about the Python-list mailing list