new.instancemethod

James_Althoff at i2.com James_Althoff at i2.com
Tue May 15 23:21:12 CEST 2001


Alex writes:

><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?

Not really.  I would say it's (the very minor point of) the use of "X.f" in
the "<unbound method X.f>" string that gives the impression at first glance
-- to me at least -- that "f" is an attribute of "X".  Especially in light
of the following:

>>> class X:
...   def f(): pass
...
>>> X.f
<unbound method X.f>

in which case "f" really *is* an attribute of "X"; but, of course, in this
case the value of "f" (in X.__dict__) is *not* an unbound method (but a
function).  I guess what "<unbound method X.f>" means in both cases is
something like "references (case 1) or results in (case 2) an unbound
method that references class (with __name__ ==) 'X' and function (with
func_name ==) 'f'".  Granted, the phrase "X.f" is shorter.  ;-)

Jim






More information about the Python-list mailing list