[Types-sig] MetaClasses (Non-ASCII art)

M.-A. Lemburg mal@lemburg.com
Wed, 02 Dec 1998 17:34:11 +0100


Just van Rossum wrote:
> 
> At 10:40 AM +0100 12/2/98, M.-A. Lemburg wrote:
> >It does... also looks a lot like what I posted to the egroups
> >list. I'll just repost it here for simplicity:
> 
> Thanks, that is an interesting approach, which actually looks a lot like
> the current class/type scheme...

Right, it's not much different, only unifies some things by
adding one more level of indirection.

> The major difference to my (possibly
> wrong) view is that I think (actually, hope) your v.__meta__ is the same
> object as v.__class__.__class__. So behavior always comes from one class
> higher up in the class chain. Maybe this is just dumb. I'll find out...

v.__meta__ points to the meta object implementing the
instance behaviour (which is different from class behaviour),
while v.__class__.__meta__ points to a different object
implementing the class behaviour of class C (== v.__class__).

Both meta objects inherit methods and attributes from Object
which would be the root of all things in the Python universe.

> >Here is a sample of the implied protocol:
> >
> >Say you want to access the attribute 'v.a':
> >
> >v.a
> 
> Minor nitpick:
> 
> >--> v.__meta__.__getattr__(v,'a')
> 
> You can't call it __getattr__, since that means getting an attr from
> __meta__.

Agreed. It is a bit confusing, especially since that __getattr__
is always called and doesn't really implement the usual __getattr__
behaviour (which would be getting an attribute from v.__meta__ as
you correctly stated).

> I propose __meta_getattr__. I find Barry's __findattr__
> confusing, too, since it reminds me of JPython's use of findattr, which, if
> I understood correctly, means something else again...

That's a good idea: all special hooks needed could be given a
'meta_' prefix to indicate that these work in different ways
than the standard ones.

> I'm not sure why inheritance needs te be brought up here (or in any
> metaclass discussion) at all: how exactly it works is to be defined by
> class.__meta__.

Inheritance for meta objects can be very useful, e.g. in case
all you want is to refine say the __meta_getattr__ method of
Class to enable automatic acquisition or caching. You won't
have to copy all the other implementation details: inheritance
does this for you.

> What I'm really saying is, if you define
>         getattr(object, attr)
> as
>         object.__meta__.__meta_getattr__(object, attr)
> or as
>         object.__class__.__class__.__meta_getattr__(object, attr)
> You're basically done. The rest is implementation details... Now, which one
> will it be?

Not really: classes and instances do different things to get
at their attributes. Instances revert to their classes for help
if they can't find an attribute, while classes try their
base classes. This means two different implementations, so
v.__meta__.__meta_getattr__ will in fact call getattr(v.__class__,name)
which is implemented by v.__class__.__meta__.__meta_getattr__.

-- 
Marc-Andre Lemburg                               Y2000: 394 days left
---------------------------------------------------------------------
          : Python Pages >>> http://starship.skyport.net/~lemburg/  :
           ---------------------------------------------------------