Is it possible to create user defined data types in python without using class, like using structure. Please explain, because I am new in python. Thank You
1 Answer
To create a type without using a class statement, you can use the type builtin:
class type(name, bases, dict)With three arguments, return a new type object. This is essentially a dynamic form of the
classstatement. The name string is the class name and becomes the__name__attribute; the bases tuple itemizes the base classes and becomes the__bases__attribute; and the dict dictionary is the namespace containing definitions for class body and is copied to a standard dictionary to become the__dict__attribute. For example, the following two statements create identicaltypeobjects:class X: a = 1 X = type('X', (object,), dict(a=1))See also Type Objects.