[Python-Dev] Class Methods

Barry A. Warsaw barry@digicool.com
Fri, 20 Apr 2001 12:36:26 -0400


>>>>> "GvR" == Guido van Rossum <guido@digicool.com> writes:

    GvR> Let x be an object, C its class, and M C's class.  So,

    |   x.__class__ is C
    |   C.__class__ is M

    GvR> Then x's methods are described in C.__dict__, and C's methods
    GvR> are described in M.__dict__.

    GvR> The problem is that if you write C.spam, there could be two
    GvR> spams: one in C.__dict__, one in M.__dict__.  Which one to
    GvR> use?

If you use naming to generally distinguish, and have a lookup chain
that first found it in C.__dict__ and then looked in M.__dict__, you
could control what happens when the name is in both dicts by using a
more explicit lookup, e.g. C.__dict__['meth']
vs. C.__class__.__dict__['meth']

But maybe that's too ugly.
    
    GvR> How does Smalltalk resolve this?

I don't remember, but I do remember that ObjC had the same concepts,
and it used a distinguishing marker on the method definition to say
whether the method was a class method (`+') or instance method (`-'),
e.g.

    + void some_class_method ...
    - void an_instance_method

Another question: presumably when I write

    class Foo: pass

Foo is implicitly given the built-in metaclass M, but say I wanted to
define a class Foo with a different metaclass, how would I spell this?
I think at one point I suggested a semi-ugly syntactic hack, where
`class' was actually a namespace and you could add new metaclasses to
it.  So you could write something like

    class.WeirdClass Foo: pass

and now Foo's metaclass would be WeirdClass.

waiting-for-the-bottom-turtle-to-burp-ly y'rs,
-Barry