Really stupid question regarding PEP 252 and type/class unification

Russell Turpin russell_turpin at hotmail.com
Thu Aug 23 21:11:54 EDT 2001


Neil Schemenauer <nas at python.ca> wrote in message news:<mailman.998582617.17836.python-list at python.org>...
> The object "lst" does not have some hidden attribute that 
> is a list. It really is itself a list object.  That's the 
> way things are implemented. Using some other mental model 
> is bound to get you into trouble sooner or later.

This gets to the nub of the matter. Let's take your example:

    class MyList(list):
        pass

    lst = MyList()

So "lst" is really a list. But it also is really an object,
instantiated from a class. So one can do things like:

    lst.a = "I just created attribute 'a'."
    lst.b = []

This is correct, right? Or is the idea that we will have two
species of classes, "types," which don't allow statements like
the above, and "true classes" which do? Anyway, assuming my
extension to the example is allowed, it now looks to me that
"lst" is (has been promoted to?) an object with three 
attributes:

    x.a == "I just created attribute 'a'."
    x.b == []
    x == []           # The default attribute

There is something that bothers me about this, but with my
sore forehead, I am unable to articulate it. Of course, I'm
even more distressed if it turns out that assigning attributes
to lst is not allowed, since this violates what I know about 
how "class" works. (And since we can now assign attributes 
even to lowly functions, why not to a type-derived class??)
If the intended model is the "two classes of class," can they 
be mixed in multiple inheritance?

    class Centaur(SomeTrueClass, MyList):
        pass

Will this generate an error? Or what about:

    class Mosaic(SomeTrueClass, dictionary):
        pass

And if I can't mix true classes with types, can I mix types?

    class Confused(dictionary, int):
        pass

    spegg = Confused()

Is "spegg" really a dictionary? Really an int? Really an 
object? Or is all of this disallowed, and we now have some 
fairly complex rules about how you may or may not use two 
classes of class? 

Oh, my poor forehead!

Russell



More information about the Python-list mailing list