object is subclassed during dynamic type creation but not during classic class definition in python2
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Horror Game Menu Looping
--
Chapters
00:00 Question
01:45 Accepted answer (Score 5)
03:27 Thank you
--
Full question
https://stackoverflow.com/questions/5645...
Question links:
[due to new style classes]: https://stackoverflow.com/a/45062077/323...
[we also know]: https://docs.python.org/3/library/functi...
[python's official documentation]: https://docs.python.org/3/library/functi...
Accepted answer links:
[types.ClassType]: https://docs.python.org/2/library/types....
[New-style and classic classes]: https://docs.python.org/2/reference/data...
--
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__).