Using function parameters to determine method kind

Michele Simionato michele.simionato at poste.it
Wed Apr 7 04:18:55 EDT 2004


Lenard Lindstrom <len-1 at telus.net> wrote in message news:<u0zwyeq2.fsf at telus.net>...
> By descriptor I mean wrapper classes like staticmethod. A naked function
> can only be an instance method descriptor, which by convention has
> a first parameter of 'self'. __new__ is exceptional in that it is an
> instance method that is always called as an unbound method. I see though
> that __new__ would break if accidentally made a class method.

Descriptors are a protocol. If an object has a __get__ method it is a
descriptor. Python functions, methods, staticmethods, classmethods,
etc.
are all descriptors. A naked function is a descriptor, even if a 
different descriptor from a bound/unbound method or a
staticmethod/classmethod; it can be converted in a staticmethod or a
classmethod or other user-defined
of descriptors. __new__ is a function which is automatically converted
to
a staticmethod and in this sense it is a special case (whereas
__init__ is
not). I think you already know all that, it was just to fix the
terminology.

> But isn't type.__call__ the factory function for
> all new-style objects, not just classes?


type.__call__ is tricky since "type" is its own metaclass. 
What do you have in mind exactly? Classes (as opposed to poor
man instances that cannot be instantiated) are made by type.__new__
which is called by type(type).__call__ which is type.__call__.

This snippet should explain the dynamics of meta-metaclasses (which
I think you understand already, but your explanation is a bit
confusing):

class MM(type):
    def __call__(mcl,name,bases,dic):
        print "calling MM.__call__"
        return super(MM,mcl).__call__(name,bases,dic)


class M(type):
    __metaclass__=MM
    def __new__(mcl,name,bases,dic):
        print "calling M.__new__"
        return super(M,mcl).__new__(mcl,name,bases,dic)


class C(object):
    """This class is created by the metaclass M, so M.__new__ is
called,
    but not directly. First type(M).__call__ is called, i.e.
MM.__call__"""
    __metaclass__=M

The output is:

calling MM.__call__
calling M.__new__

> All this talk about python functions just being another kind
> of descriptor makes me wonder if it is not time to revive
> the idea of having type function subclassable:

You may want to look at this thread:

http://groups.google.it/groups?hl=it&lr=&ie=UTF-8&threadm=95aa1afa.0402262158.5b33de79%40posting.google.com&rnum=1&prev=/groups%3Fhl%3Dit%26lr%3D%26ie%3DISO-8859-1%26q%3Dsimionato%2Bfeature%2Brequest%26btnG%3DCerca%2Bcon%2BGoogle%26meta%3Dgroup%253Dcomp.lang.python.*



More information about the Python-list mailing list