new.instancemethod

Alex Martelli aleaxit at yahoo.com
Wed May 16 07:22:21 EDT 2001


<James_Althoff at i2.com> wrote in message
news:mailman.989961743.30913.python-list at python.org...
    ...
> 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

But if you now do, as in my previous post:

    >>> gg = X.f
    >>> del X.f
    >>> gg
    <unbound method X.f>

although X has no attribute 'f' any more.  If you generate gg so
it never WAS an attribute of X, its repr is the same as if it WAS
once "an attribute of X" (or, as you point not, not quite that...:

> case the value of "f" (in X.__dict__) is *not* an unbound method (but a

...) but now is no more

> 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.  ;-)

Not sure what the start of this, "references (case 1)" &c, means
here: repr() deals with the OBJECT, not with references to it,
with how it's computed, etc.  The object in question is an unbound
method, call it U, such that:

    U.__name__ == 'f' and U.im_class.__name__ == 'X'


'X.f' is a reasonable shortcut for this, particularly given that
*most often* a reference to U will be obtained by syntax X.f --
programmers who go around using module new, calling del on
attributes of class objects, etc, can reasonably be presumed
to know what they're doing (if they don't, they're in trouble
anyway, and tweaking a repr() won't really help them much:-).


Alex






More information about the Python-list mailing list