Привет, хочу сделать переопределение плюса либо с двумя объектами либо с одним исходя из флага? Что я делаю не так?
# -*- coding: utf-8 -*-
class math_operations:
def __init__(self, x, y):
self.x = x
self.y = y
print("\nobject inititalized\n")
def __add__(self, other): # self -> first instance = obj, other -> second instance = obj2
print (self.x + other.y * 10)
class child_math_operations(math_operations):
def __init__(self, x1, y1, flag=0):
self.x1 = x1
self.y1 = y1
self.flag = flag # for choose
print("\nsecond type of object initialized\n")
if self.flag > 0:
obj_inside_1 = super().__init__(self.x1, self.y1)
obj_inside_2 = super().__init__(10, 20)
q = obj_inside_1 + obj_inside_2
print(q)
else:
def __add__(self): # self -> first instance = obj, other -> second instance = obj2
return self.x1 + self.y1 * 10
def main():
obj = math_operations(1, 2)
obj2 = math_operations(0.1, 0.2)
c = obj + obj2
print(c)
obj_0_1 = child_math_operations(100, 200, 0)
x = obj_0_1 + obj_0_1
print(x)
if __name__ == '__main__':
main()
Ошибка:
C:\Users\etimerbulatov\AppData\Local\Programs\Python\Python37\python.exe C:/proj_2/override.py
Traceback (most recent call last):
object inititalized
File "C:/proj_2/override.py", line 42, in <module>
object inititalized
3.0
None
second type of object initialized
main()
File "C:/proj_2/override.py", line 38, in main
x = obj_0_1 + obj_0_1
File "C:/proj_2/override.py", line 11, in __add__
print (self.x + other.y * 10)
AttributeError: 'child_math_operations' object has no attribute 'x'
Process finished with exit code 1