0

In the project I am working on, I have a super class with a bunch of default default parameters. Many of the parameters are custom types. Whenever I initialize a subclass, I would expect new instances of custom types to be created for each instance of the subclass, however that doesn't seem to be the case.

I was able to reproduce what I am talking about:

class custom_type:
    def __init__(self, x,y):
        self.x = x
        self.y = y

class super_class:
    def __init__(self, j=custom_type(0,0)):
        self.j = j

class sub_A(super_class):
    def __init__(self):
        super().__init__()

class sub_B(super_class):
    def __init__(self):
        super().__init__()

a = sub_A()
b = sub_B()

a.j.x = 4

print('a:', id(a.j), a.j.x, a.j.y)
print('b:', id(b.j), b.j.x, b.j.y)

output:

a: 139627050161712 4 0
b: 139627050161712 4 0
Ethan Smith
  • 67
  • 1
  • 7

0 Answers0