Don't know what I did wrong in class inheritance/special methods - python

I'm trying to solve a problem in my class, and I'm not sure what I'm doing wrong.
class Company:
def __init__(self, name=None):
self.name = name
class Travel(Company):
def __init__(self, name=None):
self.name = name
if name == None:
name = "Generic"
super().__init__(name)
def __str__(self, name=None):
self.name = name
return "Company name:{}".format(name)
def __repr__(self):
return "Travel('{self.name}')"
def set_name(self, new_name):
self.new_name = new_name
return new_name
bever = Travel('bever')
print(bever)
bever.set_name('beverly hills')
print(bever)
I want it to return
Company name: bever
Company name: beverly hills
but it just returns
Company name: None
Company name: None
any help is appreciated

You need to change a few things:
Check if name is None before you assign it to self.name
f"Travel('{self.name}')", you forgot to add an f while returning the string in __repr__
Assign new_name to self.name when you use .set_name()
class Company:
def __init__(self, name=None):
self.name = name
class Travel(Company):
def __init__(self, name=None):
if name == None:
name = "Generic"
self.name = name
super().__init__(name)
def __str__(self, name=None):
#self.name = name
return "Company name:{}".format(self.name)
def __repr__(self):
return f"Travel('{self.name}')"
def set_name(self, new_name):
self.name = new_name
return self.name
bever = Travel('bever')
print(bever)
bever.set_name('beverly hills')
print(bever)

This should do the trick
class Company:
def __init__(self, name=None):
self.name = name
class Travel(Company):
def __init__(self, name=None):
if name == None:
name = "Generic" # First set the local parameter
self.name = name # Then the attribute
super().__init__(name)
def __str__(self): # str() does not expect parameters
return f"Company name:{self.name}" # use the instance's attribute instead
def __repr__(self):
return f"Travel('{self.name}')"
def set_name(self, new_name):
self.name = new_name # Update the instance's attribute
return new_name
bever = Travel('bever')
print(bever)
bever.set_name('beverly hills')
print(bever)

Related

adding objects to a class list, from another class in Python

How could I modify the classes below so that when a new instance of Pet
is created, it is automatically added to its Owner's list of pets?
class Name:
def __init__(self, first, last):
self.first = first
self.last = last
class Pet:
def __init__(self, name, owner):
self.name = name
self.owner = owner
class Owner:
def __init__(self, name):
self.name = name
self.pets = []
owner1 = Owner(name="Juan")
pet = Pet(name="foo", owner=owner1)
owner1.pets += pet
or change your pet init
class Pet:
def __init__(self, name, owner):
self.name = name
self.owner = owner
owner.pets.append(self)

How can i create a instance and add the instance to another class in python

It is my second-day learning object-oriented programming.
I have a code which when I create a instance it should automatically be added to another class.
The way Iam asking may be wrong so apologies.
class Name:
def __init__(self, first, last):
self.first = first
self.last = last
class Pet:
def __init__(self, name, owner):
self.name = name
self.owner = owner
class Owner:
def __init__(self, name):
self.name = name
self.pets = []
How should I modify the classes above so that when a new instance of Pet
is created, it is automatically added to its Owner's list of
pets.
for example: if I created the instances below
owner_1 = Owner(Name("Daenerys", "Targaryan"))
owner_2 = Owner(Name("John", "Snow"))
pet_1 = Pet(Name("Drogon", "Targaryan"), owner_1)
pet_2 = Pet(Name("Viserion", "Targaryan"), owner_1)
pet_3 = Pet(Name("Rhaegal", "Snow"), owner_2)
Owner.pets will return a list of pets of an owner
i.e for Daenerys, the list will be ["Drogon Targaryan", "Viserion Targaryan"]
You can do something like this
class Name:
def __init__(self, first, last):
self.first = first
self.last = last
class Pet:
def __init__(self, name, owner):
self.name = name
self.owner = owner
self.owner.add_pet(self)
class Owner:
def __init__(self, name):
self.name = name
self.pets = []
def add_pet(self, pet):
self.pets.append(pet)

How would I print "bark" using python classes in this situation?

How would I print "bark" using python classes.
class pet:
def __init__(self, name, age):
self.name = name
self.age = age
def get_name(self):
return self.name
def get_age(self):
return self.age
class dog(pet):
def __init__(self, name, age):
super().__init__(name, age)
def bark(self):
print("bark")
max = pet("max", 5)
max.dog.bark()
To call the dog class's bark method, you need an instance of dog.
max = dog("max", 5)
max.bark()
Here you go.
class pet:
def __init__(self, name, age):
self.name = name
self.age = age
def get_name(self):
return self.name
def get_age(self):
return self.age
class dog(pet):
def __init__(self, name, age):
super().__init__(name, age)
def bark(self):
print("bark")
my_pet = dog("max",3)
my_pet.bark()

Super() doesn't keep the parent's class info

Trying to understand super() I made these two examples but they return the same results.
This is with super()
class Person1():
def __init__(self, name):
self.name = name
class EmailPerson1(Person1):
def __init__(self, name, email):
super().__init__(name)
self.email = email
bob2 = Person('Dim')
bob = EmailPerson1('Bob Frapples', 'bob#frapples.com')
bob.name
'Bob Frapples'
and this without super()
class Person():
def __init__(self, name):
self.name = name
class EmailPerson(Person):
def __init__(self, name, email):
self.name = name
self.email = email
bob2 = Person('Dim')
bob1 = EmailPerson('Bob Frapples', 'bob#frapples.com')
bob1.name
'Bob Frapples'
What is the difference? The first version should use the name from the parent class I think.
The difference is you are manually duplicating the work done by Person.__init__ in EmailPerson.__init__, instead of using super().__init__ to make sure the inherited initializer does what it needs to.
class Person():
def __init__(self, name):
self.name = name
class EmailPerson(Person):
def __init__(self, name, email):
self.name = name
self.email = email
Using super, i.e.,
class EmailPerson(Person):
def __init__(self, name, email):
super().__init__(name)
self.email = email
means that if someone updated the definition of Person to do some additional work in Person.__init__:
class Person:
def __init__(self, name):
self.name = name.title() # bob grapples -> Bob Frapples
you don't need to update the definition of EmailPerson at all to take advantage of the new change.

Passing class parameters python

I have 2 classes
class Robot1:
def __init__(self, name):
self.name = name
def sayHi(self):
return "Hi, I am " + self.name
class Robot2:
def __init__(self, name):
self.name = name
def sayHello(self):
return "Hello, I am " + self.name
robot_directory = {1: Robot1(), 2: Robot2()}
def object_creator(robo_id, name):
robot_object = robot_directory[robo_id]
return robot_object
But I don't know how to pass the variable name while instantiating the class on the line robot_object = robot_directory[robo_id]. How can I pass the variable?
You are storing already-created instances in the dictionary. Store the class itself instead:
# ...
robot_directory = {1: Robot1, 2: Robot2}
def object_creator(robo_id, name):
robot_class = robot_directory[robo_id]
# Here, the object is created using the class
return robot_class(name)
Obviously, this requires that all your robot classes have the same __init__ parameters.
Going further, you might want to look into inheritance and use a common base class for your robots.
maybe you can try
class Robot1:
def __init__(self):
pass
def set_name(self, name):
return "Hi, I am " + name
class Robot2:
def __init__(self):
pass
def set_name(self, name):
return "Hello, I am " + name
robot_directory = {1: Robot1(), 2: Robot2()}
def object_creator(robo_id, name):
robot_object = robot_directory[robo_id]
return robot_object.set_name(name)

Categories

Resources