0

The following code,

class A:
  pass

class B:
  member = A()

  def hello(self):
    print(repr( type(self).member ))

b1 = B()
b2 = B()

b1.hello()
b2.hello()

gives the following output:

<__main__.A object at 0x0000025193C03DA0>
<__main__.A object at 0x0000025193C03DA0>

It seems that python only instantiate A once and uses its value for all the instances of B. Why does it happen and is there a way of instantiating different global members for all instances of B ?

Xatyrian
  • 1,364
  • 8
  • 26

0 Answers0