I am starting to learn the Python language and need a critique of my code as well as solution to an error message I am getting.
The code creates a Robot class with two ancestors; Transformer and Gundam. A third class Hybrid inherits from these. I can instantiate a Transformer and a Gundam without error. Instantiating a Hybrid returns an error. Code and error follow below:
Main.py
from multiple_inheritance import Robot, Transformer, Gundam, Hybrid
tito = Gundam("Tito", "Japan")
optimus = Transformer("Optimus Prime", "Mars")
Jaeger = Hybrid("Striker", "US")
print(tito)
print(optimus)
multiple_inheritance
class Robot(object):
"""A robot class"""
def __init__(self, name):
self.name = name
def __str__(self):
return "{name}".format(name = self.name)
class Transformer(Robot):
"""A transformer"""
def __init__(self, name, planet):
self.planet = planet
super(Transformer, self).__init__(name)
def __str__(self):
return "Name = {name}, Planet = {planet}".\
format(name = self.name, planet = self.planet)
class Gundam(Robot):
"""A Gundam"""
def __init__(self, name, country):
self.country = country
super(Gundam, self).__init__(name)
def __str__(self):
return "Name = {name}, Country = {country}".\
format(name = self.name, country = self.country)
class Hybrid(Transformer, Gundam):
"""Ultimate Robot"""
Error Message
Traceback (most recent call last):
File "D:\Tech\Python\my_code\unit10\multiple_inheritance_main.py", line 5, in <module>
Jaeger = Hybrid("Striker", "US")
File "D:\Tech\Python\my_code\unit10\multiple_inheritance.py", line 13, in __init__
super(Transformer, self).__init__(name)
TypeError: __init__() missing 1 required positional argument: 'country'
Robot class with two ancestorsincorrect. In your code,Robotis the ancestor of bothTransformerandGundam