Some code.
In [1]: A = type('B', (), {})
In [2]: a = A()
In [3]: b = B()
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
/home/shabda/<ipython console> in <module>()
NameError: name 'B' is not defined
What does first argument to type doing here?
It’s setting the
__name__property of the created class.When you say:
two things happen with that ‘B’:
The name ‘B’ is assigned the class. This is just like if you’d said “
B = ...“.The
__name__property of the class is set to ‘B’.When you invoke the type constructor manually only the latter half is done. If you don’t assign the result to ‘B’ then B will remain set (or unset) as it was before.
Note that a similar result could be obtained by saying:
Now A refers to a class that calls itself ‘B’, and B doesn’t refer to anything.