There is no object
type in Python 2.1.x or lower.
Here are three files:
#!/usr/bin/env python2
class foo: pass
print(foo)
print(repr(foo))
print(type(foo))
#!/usr/bin/env python2
class foo(object): pass
print(foo)
print(repr(foo))
print(type(foo))
#!/usr/bin/env python3
class foo(object): pass
print(foo)
print(repr(foo))
print(type(foo))
and here is their outputs:(for reference only)
__main__.foo
<class __main__.foo at 0xf7d30e9c>
<type 'classobj'>
<class '__main__.foo'>
<class '__main__.foo'>
<type 'type'>
<class '__main__.foo'>
<class '__main__.foo'>
<class 'type'>
In Python 3, all types are classes.
But in Python 1 and Python 2, types are “type” type, and classes are “classobj” type. And the class generated by class foo(object): pass
is just a child class from “object” type.
So, Is it possible to create a new type(but not a new class) in Python 1 or Python 2? If it is, how?
CodePudding user response:
Thanks to @chepner
No, that's why new-style classes were added: to eliminate the artificial distinction between classes and types. Old-style classes have type
classobj
, but they were effectively deprecated as soon as new-style classes were introduced in Python 2.2. Given the extreme age of the versions being asked about, this would probably be more suitable retrocomputing.stackexchange.com.