Need a better understanding on how MRO works?

Alex Martelli aleax at mac.com
Sun Aug 26 01:14:36 EDT 2007


Steven W. Orr <steveo at syslang.net> wrote:
   ...
> Thanks Alex. I am humbled, though I was before I started.
> I really don't have a lot of understanding of what you're saying so I'll
> probably have to study this for about a year or so.
> 
> * (I need to look up what dictproxy is.) I don't have any idea what the
> ramifications are of your use of the word DISTINCT. Are you somehow 
> suggesting that new.classobj does a deep copy of the globals copy that's
> passed to it?

No, most definitely NOT deep!!!, but type.__new__ does "a little" of
what you've said (a shallow copy, which is not quite "a copy" because it
embeds [some of] the entries in slots).  new.classobj determines the
metaclass (from the bases, or a __metaclass__ entry in the dictionary)
and calls it to generate the new class.  For modern style classes, the
class is type; for old-style legacy classes, it's types.ClassType, and
they're not exactly identical in behavior (of course not, or there would
no point in having both:-).

> 
> * Also, I'd like to understand what the difference is between 
>     nclass = new.classobj(name,(D1,),globals())
> vs. 
>     def classfactory():
>       class somename(object):
>           def somestuff():
>               pass
>         return somename
>     G1 = classfactory()
>     globals()[name] = G1
> 
> Does new.classobj do anything special?

No, new.classobj does essentially the same thing that Python does after
evaluating a class statement to prepare the class's name, bases and
dictionary: finds the metaclass and calls it with these arguments.

A key difference of course is that a class statement prepares the class
dictionary as a new, ordinary, distinct dictionary, while new.classobj
accepts whatever dictionary you give it (so you can, though shouldn't,
do strange things such as pass globals()...:-).


Alex



More information about the Python-list mailing list