0

I used Sven Marnach's answer to overwrite my class. But some methods or values in 'super' or 'super-of-super' class were missing, depending on executing __class__ or __dict__ lines.

This is a simple code that represents the problem.

class Grand:
  def __init__(self, num):
    self.g = num
  def gg(self):
    print("Grand:", self.g)

class Parent(Grand):
  def __init__(self, num):
    super().__init__(num)
    self.p = num
  def pp(self):
    print("Parent:", self.p)
    print("Grand:", self.g)

class Child(Grand):
  def __init__(self, A, B, C):
    A.p = A.p - B.p - C.p
    self.__class__ = A.__class__ ###!!!
    self.__dict__ = A.__dict__   ###!!!
  def cc(self):
    print("New value:", self.p)

p1 = Parent(100)
p2 = Parent(20)
p3 = Parent(30)
c = Child(p1, p2, p3)
print(dir(c))

### Both __class__ and __dict__ updated: 'cc' mising ###
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'g', 'gg', 'p', 'pp']
Traceback (most recent call last):
  File "toy.py", line 28, in <module>
    c.cc()
AttributeError: 'Parent' object has no attribute 'cc'

### Only __class__ updated: 'pp' missing ###
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'cc', 'g', 'gg', 'p']
New value: 50

### Only __dict__ updated: 'g', 'p', 'cc' missing ###
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'gg', 'pp']
Traceback (most recent call last):
  File "toy.py", line 28, in <module>
    c.cc()
AttributeError: 'Parent' object has no attribute 'cc'

### No updated 'g', 'p', 'pp' missing ###
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'cc', 'gg']
Traceback (most recent call last):
  File "toy.py", line 28, in <module>
    c.cc()
  File "toy.py", line 21, in cc
    print("New value:", self.p)
AttributeError: 'Child' object has no attribute 'p'

What are the proper ways to update all values: 'g', 'p' and methods: 'pp', 'gg', 'cc'?

Noki Lee
  • 1
  • 2

1 Answers1

0

Please try omitting the below line from Child __init__ function: self.class = A.class

It will give you the function 'g' as well as 'f' in result of 'c' instance.

I hope it will help you.

Suman100
  • 11
  • 4