7
7stud
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:
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: