Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > class objects, method objects, function objects

Reply
Thread Tools

class objects, method objects, function objects

 
 
7stud
Guest
Posts: n/a
 
      03-19-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?

 
Reply With Quote
 
 
 
 
7stud
Guest
Posts: n/a
 
      03-19-2007
Oops, here is that last sentence in context(section 9.3.4):
------------
What exactly happens when a method is called? You may have noticed
that x.f() was called without an argument above, even though the
function definition for f specified an argument. What happened to the
argument? Surely Python raises an exception when a function that
requires an argument is called without any -- even if the argument
isn't actually used...

Actually, you may have guessed the answer: the special thing about
methods is that the object is passed as the first argument of the
function. In our example, the call x.f() is exactly equivalent to
MyClass.f(x). In general, calling a method with a list of n arguments
is equivalent to calling the corresponding function with an argument
list that is created by inserting the method's object before the first
argument.

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

 
Reply With Quote
 
 
 
 
7stud
Guest
Posts: n/a
 
      03-19-2007
Darn. I made some changes to the class and I didn't change the
function object. It should be:

|
V
function object
---------------------------------
| def sayHi(self): |
| print "Hello " + self.name|
|_________________________________|


 
Reply With Quote
 
Gabriel Genellina
Guest
Posts: n/a
 
      03-19-2007
En Mon, 19 Mar 2007 01:38:37 -0300, 7stud <(E-Mail Removed)>
escribió:

> 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?


Calling a.foo(x,y) means that, after fetching foo from a, a new argument
list must be constructed containing 3 items: the instance a (self), x and
y.

--
Gabriel Genellina

 
Reply With Quote
 
Alex Martelli
Guest
Posts: n/a
 
      03-19-2007
7stud <(E-Mail Removed)> wrote:
...
> 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?


Here's the Python pseudocode equivalent...

class Method(object):
def __init__(self, im_func, im_self):
self.im_func = im_func
self.im_self = im_self
def __call__(self, *a, **k):
return self.im_func(self.im_self, *a, **k)

Whenever you get foo.somemeth from an instance foo of whatever class
(with somemeth being a method of that class), you obtain the equivalent
of a Method(type(foo).__dict__('somemeth'), foo) in the above
pseudocode. When you call that foo.somemeth, the im_self (i.e., foo) is
prepended to other positional arguments (if any).

This, btw, takes place in the __get__ method of the function object
somemeth -- function objects are descriptors, i.e., they have __get__
methods, and that's just what they do.

If this is still a bit obscure to you, perhaps more practice with Python
may be needed before you dwell into the internals of methods &c.


Alex
 
Reply With Quote
 
Fredrik Lundh
Guest
Posts: n/a
 
      03-19-2007
"7stud" wrote:
> 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?


when you call obj.method(arg1, arg2), Python prepends the actual instance
object (obj) to the argument list, so you end up calling (obj.method) with the
arguments (obj, arg1, arg2).

</F>



 
Reply With Quote
 
7stud
Guest
Posts: n/a
 
      03-19-2007
Hi,

Thanks for the responses. I understand that python automatically
sends 'self' to a member function, i.e. self gets prepended to the
argument list. I guess I am having trouble with this statement:
----
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.
---
because I don't quite understand the timing. The "unpacked again"
part confuses me. If I have this code:

class MyClass:
def g(self, name):
print "Hello " + name

x = MyClass()
x.g("GvR")

here is what happens when I try to follow the sequence of events GvR
describes:
----------------
When an instance attribute is referenced that isn't a data attribute,
its class is searched.

x.g("GvR") ==> 'g' is not a data attribute, so 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.

==>method object is created

When the method object is called with an
argument list,

x.g("GvR") ==> I think I'm both "referencing an instance attribute"
and calling the method object with an argument list

it(the method object) 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.

??

 
Reply With Quote
 
Duncan Booth
Guest
Posts: n/a
 
      03-19-2007
"7stud" <(E-Mail Removed)> wrote:

> When the method object is called with an
> argument list,
>
> x.g("GvR") ==> I think I'm both "referencing an instance attribute"
> and calling the method object with an argument list
>
> it(the method object) 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.


It may help you to understand if you consider the two parts separately.
There is no linkage between 'referencing an instance attribute' and
'calling the method object'. You can rewrite the code as:

method = x.g
method("GvR")

and it works in exactly the same way. First you reference the attribute
'x.g', which looks up MyClass.g (an unbound method) and calls
MyClass.g.__get__(x, MyClass) which creates a new bound method. Then you
call the bound method passing "GvR" as the first positional argument: so a
single element tuple is created for the argument list.

The bound method object unpacks and repacks the tuple to give the two-tuple
(self, "GvR") which it then passes to the original function.
 
Reply With Quote
 
Dennis Lee Bieber
Guest
Posts: n/a
 
      03-20-2007
On 19 Mar 2007 10:21:42 -0700, "7stud" <(E-Mail Removed)>
declaimed the following in comp.lang.python:

> Hi,
>
> Thanks for the responses. I understand that python automatically
> sends 'self' to a member function, i.e. self gets prepended to the
> argument list. I guess I am having trouble with this statement:
> ----
> 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.
> ---
> because I don't quite understand the timing. The "unpacked again"
> part confuses me. If I have this code:
>

<snip>

> it(the method object) is unpacked again, a new argument list is


"it(the" -- argument list, not the object -- ") is unpacked again, a
new argument list is constructed from the instance object" (ie, the
object is turned into the first argument 'self') "and the original
argument list..."


object.method(arg, u, ments) is turned into

class_function(object, arg, u, ments)


Would be how /I/ read the documents.

--
Wulfraed Dennis Lee Bieber KD6MOG
http://www.velocityreviews.com/forums/(E-Mail Removed) (E-Mail Removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (E-Mail Removed))
HTTP://www.bestiaria.com/
 
Reply With Quote
 
Fredrik Lundh
Guest
Posts: n/a
 
      03-20-2007
Dennis Lee Bieber wrote:

> "it(the" -- argument list, not the object -- ") is unpacked again


no, "it" refers to the bound method object, as 7stud would have realized if
he'd read the entire paragraph. here's the relevant portion:

/.../ 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. When the method object is called with an argument list, it
is unpacked again /.../

</F>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
injecting an instance method into another class' class method Chuck Remes Ruby 3 08-12-2010 07:15 PM
why a class can't access protected method from another class in thesame package,the method is interited from the ohtner class from differntpackage? junzhang1983@gmail.com Java 3 01-28-2008 02:09 AM
how to call method of the class which contains a pointer to other class method? Pawel_Iks C++ 3 07-31-2007 06:30 AM
Confused by Method(function) of a module and method of a class/instance Sullivan WxPyQtKinter Python 5 03-07-2006 05:23 PM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM



Advertisments