Re: Why we will use obj$func() often

From: Mike C. Fletcher (mcfletch_at_rogers.com)
Date: 04/22/04


Date: Thu, 22 Apr 2004 16:21:59 -0400
To: Mark Hahn <mark@prothon.org>

Mark Hahn wrote:

>My apologies. I posted this to c.l.p. by accident. I meant to post this
>to Prothon-users.
>
>
Oh, no problem, there's some Python content (see below for some comments
on it)...
...

>># Python
>>
>>class klass:
>> def __init__(self):
>> self.me = 1
>> def func(self):
>> print "func1,self"+str(self.me),
>>
>>class klass2(klass):
>> def __init__(self):
>> self.me = 2
>> def func(self):
>> klass.func(self) # delegation
>> print "func2,self"+str(self.me),
>>
>>inst = klass2()
>>inst.func() # prints func1,self2 func2,self2
>>
>>
>>
...

>>In Python the call klass.func() was different because Python knows that
>>klass is a class and therefore obviously cannot be the target of call (an
>>instance), so it made the programmer pass the instance self to use as a
>>parameter.
>>
>>
This isn't really a very clear description of what's going on in
Python. It won't matter to the Prothon users, but don't want any Python
users to get confused...

    Looking up klass.func returns an unbound instance method, this is a
    function wrapper which basically just says "hey, you're a member
    function of a class, check to be sure that your first argument is a
    member of that class". This is done by the function's descriptor
    hooks which allow it to return a wrapped object when the user
    attempts to retrieve the value from another object (such as a class
    or an instance). So, in a sense, yes, the class cannot be the
    target of that *particular* call, as the unbound method object you
    retrieved will reject anything other than an instance of the class.

    Bound instance methods are a similar wrapper, but they say
    "curry/bind your first argument (normally self) to this value then
    call the underlying function". They are created on object.method
    access by the same function descriptor hooks.

The point of all that being that classes most definitely *can* be the
target of a call. Python's classes are first-class objects. In
particular, they are instances of metaclasses, and can have meta-methods
defined which take the class as their first parameter just like a normal
method-call.

>>> class k(type):
... def r( cls ):
... return 42
...
>>> class x:
... __metaclass__ = k
...
>>> x.r()
42
>>>

There's very little "special" about classes other than that they have
some syntactic shortcuts for creating them and for looking up attributes
of their instances within them. Python isn't looking at every method
call and saying "hey, that's a class, that can't be the first parameter
to a function/method!", it (particularly the unbound instance object) is
saying "hey, you're not an instance of my class, go to heck" and never
thinks about whether the object is *particularly* a class or not.

Classes are special in Python, but not nearly as special as you might
think from a class-less perspective :) ,
Mike

    By the way, the modern Python idiom is:

        super( klass2, self ).func( )

    but that wouldn't help in explaining the logic for the Prothon
    choice, so no biggie :) .

_______________________________________
  Mike C. Fletcher
  Designer, VR Plumber, Coder
  http://members.rogers.com/mcfletch/



Relevant Pages

  • Re: Decorators: an outsiders perspective
    ... > Because of the conventions widely used (in Python) today, ... > us will assume that that this is an instance method. ... > overloading the semantics of the first parameter. ...
    (comp.lang.python)
  • Re: static class methods and data members
    ... > about the members of the class that encloses them ... first parameter equivalent to Python's self ... ... just like in Python ... ... with staticmethods that can's be done whith classmethods ... ...
    (comp.lang.python)
  • Re: understanding self
    ... > object that takes self as its first parameter even though it's not ... Another Python design principle is to minimize the number of keywords. ... an implicit 'self' would violate the second "Zen ... to have it explicit. ...
    (comp.lang.python)
  • Re: multiple values for keyword argument
    ... create_editgot multiple values for keyword argument 'row' ... When you invoke a method Python implicitly passes the instance as the first ... At first positionals are bound to the formal parameters in the order of ... reference as first parameter everything was clear (It's just like argc and argv ...
    (comp.lang.python)
  • Re: Using function parameters to determine method kind
    ... >> a method's kind from its first parameter. ... instances of type function. ... parameter checking in the python interpreter. ... Descriptors are unaffected by the function's parameters, ...
    (comp.lang.python)