[Baypiggies] Fwd: [Tutor] what.built-in

Bob Ippolito bob at redivi.com
Thu Oct 5 04:54:54 CEST 2006


On 9/29/06, Laurence Clark <hsuclarklarry at sbcglobal.net> wrote:
>
>  And what this about "new style objects". Do they have a "new style" common
> base class?

New style classes have a metaclass of type. Usually this means they
are a subclass of object. This changes the behavior of some of the
__special_methods__ and it's a requiment for descriptors (such as the
property, staticmethod, and classmethod built-ins). The metaclass can
be set in the class dictionary (in the class body usually), or as a
global in the module that the class is defined in. Metaclasses are
also inherited, so subclasses of new style classes will be a new style
class.

>>> class NewStyle(object):
...     pass
...
>>> type(NewStyle), type(NewStyle())
(<type 'type'>, <class '__main__.NewStyle'>)


>>> class AlsoNewStyle:
...     __metaclass__ = type
...
>>> type(AlsoNewStyle), type(AlsoNewStyle())
(<type 'type'>, <class '__main__.AlsoNewStyle'>)


>>> class OldStyle:
...     pass
...
>>> type(OldStyle), type(OldStyle())
(<type 'classobj'>, <type 'instance'>)


>>> __metaclass__ = type
>>> class NewStyleFromGlobal:
...     pass
...
>>> type(NewStyleFromGlobal), type(NewStyleFromGlobal())
(<type 'type'>, <class '__main__.NewStyleFromGlobal'>)


-bob


More information about the Baypiggies mailing list