class - What is the difference between old style and new style classes in Python? -
what difference between old style , new style classes in python? there ever reason use old-style classes these days?
from http://docs.python.org/2/reference/datamodel.html#new-style-and-classic-classes :
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 ofx
,type(x)
<type 'instance'>
. reflects fact old-style instances, independently of class, implemented single built-in type, called instance.new-style classes introduced in python 2.2 unify concepts of class , type. new-style class user-defined type, no more, no less. if x instance of new-style class,
type(x)
typically samex.__class__
(although not guaranteed – new-style class instance permitted override value returnedx.__class__
).the major motivation introducing new-style classes provide unified object model full meta-model. has number of immediate 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.
python 3 has new-style classes. no matter if subclass
object
or not, classes new-style in python 3.
Comments
Post a Comment