type of class in python -
why if do:
class c(): pass type(c())
i got: <type 'instance'>
, if do:
class c(object): pass type(c())
i got: <class '__main__.c'>
?
the first not userfull
look difference between old-style , new-style classes. former default, , latter inherit explicitly object.
all old-style objects implemented built-in type instance. fact still default , type remains 'instance' result of retro-compatibility precautions.
this extracted python docs (http://docs.python.org/reference/datamodel.html)
3.3. new-style , classic classes classes , instances come in 2 flavors: old-style (or classic) , new-style.
up python 2.1, old-style classes flavour available user. concept of (old-style) class unrelated concept of type: if x instance of old-style class, x.class designates class of x, type(x) . reflects fact old-style instances, independently of class, implemented single built-in type, called instance.
new-style classes introduced in python 2.2 unify classes , types. new-style class neither more nor less user-defined type. if x instance of new-style class, type(x) typically same x> .class (although not guaranteed - new-style class instance permitted override value returned x.class).
the major motivation introducing new-style classes provide unified object model full meta-model. has number of practical benefits, ability subclass built-in types, or introduction of “descriptors”, enable computed properties.
for compatibility reasons, classes still old-style default. new-style classes created specifying new-style class (i.e. type) parent class, or “top-level type” object if no other parent needed. behaviour of new-style classes differs of old-style classes in number of important details in addition type() returns. of these changes fundamental new object model, way special methods invoked. others “fixes” not implemented before compatibility concerns, method resolution order in case of multiple inheritance.
while manual aims provide comprehensive coverage of python’s class mechanics, may still lacking in areas when comes coverage of new-style classes. please see http://www.python.org/doc/newstyle/ sources of additional information.
old-style classes removed in python 3.0, leaving semantics of new-style classes.of new-style classes.
Comments
Post a Comment