0

Here's a question about python. Apologies in advance if this is too basic.

Consider the following:

class H:
    pass


class D:
    def f(self):
        h = H()
        print("h:", h)


class C:
    def __init__(self):
        d1 = D()
        d2 = D()
        print("d1:", d1)
        print("d2:", d2)
        d1.f()
        d2.f()


c = C()

The output is the following:

d1: <__main__.D object at 0x1010fa6d8>
d2: <__main__.D object at 0x1010fa710>
h: <__main__.H object at 0x1010fa748>
h: <__main__.H object at 0x1010fa748>

Why is the H object being created the same, since it's being generated from two different instances of class D?

How can I create a different instance of H for each instance of D?

martineau
  • 119,623
  • 25
  • 170
  • 301

0 Answers0