new.instancemethod

Alex Martelli aleaxit at yahoo.com
Mon May 14 18:12:48 EDT 2001


<James_Althoff at i2.com> wrote in message
news:mailman.989869341.18958.python-list at python.org...
    ...
> >>>> met=new.instancemethod(f,None,X)
> >>>> met
> ><unbound method X.f>
    ...
> interesting . . . but odd?

No, why "odd"?  new.instancemethod is not documented to
alter any of its arguments, so:

> >>> met = new.instancemethod(f,None,X)
> >>> met
> <unbound method X.f>
> >>> dir(X)
> ['__doc__', '__module__']
> >>> X.f
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> AttributeError: f

...of course class object X is not modified.  Does it seem odd
to you to have an object met, that is an unbound method of
class X, when object X does not have met as an attribute?  That
can easily be obtained by vastly less magical approaches than
module new, of course...:

D:\ian>python
Python 2.1 (#15, Apr 16 2001, 18:25:49) [MSC 32 bit (Intel)] on win32
Type "copyright", "credits" or "license" for more information.
Alternative ReadLine 1.1 -- Copyright 2001, Chris Gonnerman
>>> class X:
...   def f(self): print 'f'
...
>>> met=X.f
>>> del X.f
>>> met
<unbound method X.f>
>>> dir(X)
['__doc__', '__module__']
>>> X.f
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
AttributeError: class X has no attribute 'f'
>>>


Alex






More information about the Python-list mailing list