functions, classes, bound, unbound?

7stud bbxx789_05ss at yahoo.com
Sun Mar 25 16:22:36 EDT 2007


On Mar 25, 3:00 am, irs... at gmail.com wrote:
> On Mar 25, 9:13 am, "7stud" <bbxx789_0... at yahoo.com> wrote:
>
> > MyClass.someFunc
>
> > Is there some other way to retrieve a user-defined function object
> > from a class other than using the class name or an instance?
>
> What Steven B. already said, MyClass.__dict__['someFunc'], is a
> different way than MyClass.someFunc that produces different results.

That doesn't seem to fit what GvR was talking about.  From this
example:

class Test(object):
        def greet():
                print "Hello"

methObj = Test.__dict__["greet"]
print methObj.im_self

I get this output:

Traceback (most recent call last):
  File "test1.py", line 7, in ?
    print methObj.im_self
AttributeError: 'function' object has no attribute 'im_self'

So it doesn't look like a method object gets created when you retrieve
a function from a class like that.  Compare to:

class Test(object):
        def greet():
                print "Hello"

methObj = Test.greet
print methObj.im_self

output:
None

> Since the method is an attribute of a class, what other kinds of means
> are you expecting to be possible?
>

I'm just wondering why in this description:
----
When a user-defined method object is created by retrieving a user-
defined function object from a class, its im_self attribute is None
and the method object is said to be unbound. When one is created by
retrieving a user-defined function object from a class via one of its
instances, its im_self attribute is the instance, and the method
object is said to be bound.
-----
GvR didn't explicitly mention using the class name to retrieve the
function object in the first sentence.  It seems to imply there are
other ways to retrieve the function object from the class that cause a
method object to be created.




More information about the Python-list mailing list