3 ms·
I was working on writing the same thing, though less clearly. :) As a minor note, in Python 2, types.ClassType was a "thing": >>> import types >>> types.C
by eesmith 6y ago
I was working on writing the same thing, though less clearly. :)
As a minor note, in Python 2, types.ClassType was a "thing":
>>> import types
>>> types.ClassType is type
False
>>> isinstance(types.ClassType, type)
True
>>> my_class = types.ClassType("spam", bases=(), dict={})
>>> my_class
<class __main__.spam at 0x106732670>
I don't remember the details any more, but it looks like it was the base for class for old-style classes. Using your class definitions:
>>> isinstance(OldStyleClass, types.ClassType)
True
>>> isinstance(NewStyleClass, types.ClassType)
False
Python 3 replaced that functionality with types.new_class(), which returns an instance of type 'type':
>>> my_class = types.new_class("spam")
>>> isinstance(my_class, type)
True
>>> my_class
<class 'types.spam'>
>>> type(my_class)
<class 'type'>
As I recall, in the early 1.x days it wasn't apparent that the class/type dichotomy was a problem. It took a lot of work during the 2.x era to bring them together.
- chrismorgan 6y agoThanks for the correction; it’s about five years since I’ve done any serious work in Python, and six or seven since I last did any involved metaprogramming, and I assiduously avoided old-style classes even then, and so I had completely forgotten about types.ClassType. Python 2.4 was the oldest version I ever worked with, also.