new.instancemethod

James_Althoff at i2.com James_Althoff at i2.com
Wed May 16 14:55:13 EDT 2001


>Alex writes:
>
>Not sure what the start of this, "references (case 1)" &c, means
>here:

Oh, I was just trying to indicate -- in an unclear way :-) -- that for the
two examples we were using: in example 1) the variable "met" directly
references an existing unbound method object whereas in example 2) "X.f"
creates an unbound method object on the fly using the X.__dict__['f']
function object.  It wasn't all that pertinent, I guess, since the
description string describes the resultant object in any case, as you point
out.

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

True.  On the other hand, Python does support instance-specific methods.
So what is a nice example usage of this capability and what is a good
practice for its implementation?  In GUI code I often want to do something
like giving "instance-specific behavior" to each button on a panel.  But in
practice, if I have two buttons, button1 and button2, lets say, I usually
assign two different handler methods, "handleButton1" and "handleButton2"
-- one to each button -- that are defined as part of some controller class.
So I'm using an instance-specific attribute (that references a bound method
-- which is extremely useful) but it's not an "instance-specific method" in
the sense that the "im_self" part of the bound method refers to the
controller object and not to the button object itself.  So, what would be a
good example of an instance-specific method where the "im_self" of the
bound method object actually points back to the object of which it is an
instance-specific method (to describe the concept as poorly as possible ;-)
).  And then, how does one build such a method without using module "new"?
Any good examples from practice?

>
>Alex

Jim






More information about the Python-list mailing list