Dynamic class methods misunderstanding

Bill Mill bill.mill at gmail.com
Fri Jan 28 12:04:05 EST 2005


On Fri, 28 Jan 2005 11:59:50 -0500, Hans Nowak <hans at zephyrfalcon.org> wrote:
> 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>
> 

d'oh; that's how I ran it. Thanks a lot.

> --
> Hans Nowak
> http://zephyrfalcon.org/
> 
> --
> http://mail.python.org/mailman/listinfo/python-list
>



More information about the Python-list mailing list