I'm trying to design a class structure where the bases of a class are determined at runtime. With a great many thanks to the answer to this question about metaclasses, it clicked that the type
function can be made to do this by constructing the tuple of bases at runtime. Excellent.
Now that opens a possibility that I'm not sure I'm comfortable with-- through some sequence of operations I could wind up with the equivalent to:
X = type('X', (object,), {})
Y = type('Y', (X,), {})
Y2 = type('Y', (object,), {})
I now have two classes (Y
and Y2
) with different inheritance structures but both share the classname 'Y'
.
c = y.__class__
c2 = y2.__class__
(c,c2) #returns (__main__.Y, __main__.Y)
(c.__bases__,c2.__bases__) #returns ((__main__.X,), (object,))
It appears from this, that the classname is just an arbitrary string for human consumption, and Python takes the view that if I want to go and confuse myself, I'm welcome to.
Is that correct? Are there pitfalls awaiting me elsewhere in the bowels of Python if different classes are allowed to share the same name?