object is subclassed during dynamic type creation but not during classic class definition in python2
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Drifting Through My Dreams
--
Chapters
00:00 Object Is Subclassed During Dynamic Type Creation But Not During Classic Class Definition In Python2
01:17 Accepted Answer Score 5
02:40 Thank you
--
Full question
https://stackoverflow.com/questions/5645...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #python3x #python27 #class
#avk47
ACCEPTED ANSWER
Score 5
Let's look into this Python 2 shell.
>>> class X1:
... a = 1
...
... X2 = type('X2', (), {'a': 1})
>>> type(X1)
0: <type 'classobj'>
>>> type(X2)
1: <type 'type'>
>>> import types
>>> assert types.ClassType is type(X1)
types.ClassType is described as:
The type of user-defined old-style classes.
Basically type in the new-style classes' default metaclass. If you want metaprogramming in the old style, you can use types.ClassType in the same way.
>>> X3 = types.ClassType('X3', (), {'a': 1})
>>> X3.__bases__
2: ()
For the reference, excerpt from Python 2's New-style and classic classes:
Classes and instances come in two flavors: old-style (or classic) and new-style.
Up to Python 2.1 the concept of
classwas unrelated to the concept oftype, and old-style classes were the only flavor available. For an old-style class, the statementx.__class__provides the class of x, buttype(x)is always<type 'instance'>. This reflects the fact that all old-style instances, independent of their class, are implemented with a single built-in type, calledinstance.New-style classes were introduced in Python 2.2 to unify the concepts of
classandtype. A new-style class is simply a user-defined type, no more, no less. If x is an instance of a new-style class, thentype(x)is typically the same asx.__class__(although this is not guaranteed -- a new-style class instance is permitted to override the value returned forx.__class__).