King Harry Ответов: 1

Проблема наследования....... Что я делаю


$ python python1.py
Traceback (most recent call last):
  File "python1.py", line 674, in <module>
    smart_phone=Smart_phone("google","pixal 5","88000","8","12","touch_screen")
  File "python1.py", line 668, in __init__
    super().__init__(self,ram,rear_camera,screen)
TypeError: __init__() takes 4 positional arguments but 5 were given


Что я уже пробовал:

class phone:
    def __init__(self,brand,model,price):
        self.brand=brand
        self.model=model
        self.price=price
    def full_name(self):
        return f"{self.brand} and {self.model}"
class Smart_phone(phone):
    def __init__(self,brand,model,price,ram,rear_camera,screen):
        super().__init__(self,ram,rear_camera,screen)
        self.ram=ram
        self.rear_camera=rear_camera
        self.screen=screen
    def full_name(self):
        return f"{self.brand} and {self.model} and "
smart_phone=Smart_phone("google","pixal 5","88000","8","12","touch_screen")
phone=phone("nokia",1100,1000)
# print(phone.full_name())
print(smart_phone.full_name())

1 Ответов

Рейтинг:
1

CPallini

Как следует из сообщения, Вы должны удалить параметр self в super().__init__ вызов (см., например, Python super() - Python 3 super() - JournalDev[^]).
Попробуй

class phone:
    def __init__(self,brand,model,price):
        self.brand=brand
        self.model=model
        self.price=price
    def full_name(self):
        return f"{self.brand} and {self.model}"
class Smart_phone(phone):
    def __init__(self,brand,model,price,ram,rear_camera,screen):
        super().__init__(brand,model,price)
        self.ram=ram
        self.rear_camera=rear_camera
        self.screen=screen
    def full_name(self):
        return f"{self.brand} and {self.model} and "
smart_phone=Smart_phone("google","pixal 5","88000","8","12","touch_screen")
phone=phone("nokia",1100,1000)
# print(phone.full_name())
print(smart_phone.full_name())