should I put old or new style classes in my book?
Arnaud Delobelle
arnodel at googlemail.com
Fri May 30 04:03:32 EDT 2008
Alan Isaac <aisaac at american.edu> writes:
> This thread raises two questions for me.
>
> 1. I take it from this thread that in Python 3 the following are
> equivalent:
>
> class Test: pass
>
> class Test(object): pass
>
> Is that correct, and if so, where is it stated explicitly?
> (I know about the "all classes are new style classes" statement.)
I don't know where it is stated, but how could they *not* be
equivalent?
> 2. I take it from this thread that in Python 2.2+ if I put the
> following at the top of a module ::
>
> __metaclass__ = type
>
> then all the classes defined in that module will be newstyle
> classes. Is that correct? Somehow I did not grok that from
>
> <URL:http://docs.python.org/ref/metaclasses.html>
>
> but it seems right.
>From the URL you quote:
The appropriate metaclass is determined by the following
precedence rules:
* If dict['__metaclass__'] exists, it is used.
* Otherwise, if there is at least one base class, its metaclass is
used (this looks for a __class__ attribute first and if not
found, uses its type).
* Otherwise, if a global variable named __metaclass__ exists, it
is used.
* Otherwise, the old-style, classic metaclass (types.ClassType) is
used.
Look at the third point.
--
Arnaud
More information about the Python-list
mailing list