class objects, method objects, function objects

7stud bbxx789_05ss at yahoo.com
Mon Mar 19 00:38:37 EDT 2007


Hi,

I'm trying to figure out what this passage from GvR's tutorial means:
-------
Class definitions, like function definitions (def statements) must be
executed before they have any effect....

When a class definition is entered, a new namespace is created...

When a class definition is left normally (via the end), a class object
is created.

If you still don't understand how methods work, a look at the
implementation can perhaps clarify matters. When an instance attribute
is referenced that isn't a data attribute, its class is searched. If
the name denotes a valid class attribute that is a function object, a
method object is created by packing (pointers to) the instance object
and the function object just found together in an abstract object:
this is the method object.
-----------

Here is my diagram of the above:

               class object
   ----------------------------------------
  |  class MyClass:                        |
  |      def __init__(self):               |
  |            self.name = "GvR"           |
  |      def sayHi(self):                  |
  |            print "Hello " + self.name  |
  |________________________________________|


x = MyClass()
x.sayHi()  #the "class search" begins which initiates the
           #creation of the method object:


            method object              instance object
          -----------------            --------------
         |  ptr1 ----------|--------> |x = MyClass() |
         |  ptr2           |          |______________|
         |   |             |
         |___|_____________|
             |
             |
             V
       function object
 ---------------------------------
|  def sayHi(self): print "hello" |
|_________________________________|

But the last part of the passage makes no sense to me:
------
When the method object is called with an argument list, it is unpacked
again, a new argument list is constructed from the instance object and
the original argument list, and the function object is called with
this new argument list.
------
Can anyone interpret that for me?




More information about the Python-list mailing list