0

I have a quiestion on generating classes in Python with type() function

class A:
    class B:
        some_field = some_value

object received with A constructor call is as follows:

a = <__main__.A object at 0x040BF730>
    B = <class '__main__.A.B'>
        some_field = 0

Then I tried to make the same class with type() function

C = type("C", (), { 'D': type('D', (), {'some_field': int()})})

Object received with C constructor call contains D which is not inherited from C obviously:

c = <__main__.C object at 0x040BF930>
    D = <class '__main__.D'>
        some_field = 0

Is there a method to make the same structure as A() with factory function?

Dmitry
  • 49
  • 1
  • 7
  • 3
    Nested classes don't inherit to begin with. `__main__.A.B` does not mean `B` inherits from `A`, it just means its fully qualified name is `A.B`! – deceze Apr 10 '20 at 08:15
  • Oh, got it. But question is still about how to make **the same** structure as A() even it's name, not inheritance property. – Dmitry Apr 10 '20 at 08:19
  • 1
    The first argument to `type` decides its name, so `type('C.D', ...)`. – deceze Apr 10 '20 at 08:24

0 Answers0