Python - Constructing new class

Posted on Thu 16 March 2017 in Python, type

For constructing new class using type

 >>> cls = type('NameOfClass', (object,), {})

To check name of new class

>>> cls.__name__
'NameOfClass'

Specifying __doc__, __name__ and __module__

>>> classbody = dict(__doc__="Sample Doc", __name__="N", __module__="modename")
>>> cls = type('N', (object,), classbody)
>>> cls.__doc__, cls.__name__, cls.__module__
('Sample Doc', 'N', 'modename')