Instances as return value
Related code
class Dog: """Represents a dog. attributes: name, age and collar """ class Collar: """Represents a collar. attributes: color and size """ def print_dog_info(dog): print(f"Name: {dog.name}, age: {dog.age}") print(f"{dog.name}'s collar color: {dog.collar.color}, {dog.name}'s collar size: {dog.collar.size}")
Functions can return instances. For example, create_collar takes a color and a size, and returns a collar of such color and size:
... def create_collar(color, size): c = Collar() c.color = color c.size = size return c
Here is an example of its usage:
... dog = Dog() dog.name, dog.age, dog.collar = "Murki", 2, create_collar('black', 5)
create_collar('black', 5)
returned a Collar
instance, and it was directly
assigned to dog
's collar
attribute.
>>> dog.collar <Collar object at 0x00000148DF62B2D0> >>> dog.collar.color, dog.collar.size ('black', 5) >>>
Exercises
class Cow: """Represents a cow""" class Robot: """Represents a robot""" def print_robot_cow(robot): print("name: {}, pet's name: {}".format(robot.name, robot.pet.name)) print("{}'s pet is {} years old.".format(robot.name, robot.pet.age))
Define a function, create_cow, which takes one string and one integer, name and age, creates a Cow object, assigns name to object's name attribute and age to object's age attribute, and returns the object
Create a robot, robot3, and add name attribute to it, which is a string representing robot's name
Call create_cow, passing the required arguments, and assign it's return value (directly) to pet attribute of robot3
Call create_author, passing the required arguments, and assign it's return value directly to author attribute of created book
class Bus: pass class Driver: pass def assign_driver(name): d = Driver() d.name = name return d b = Bus() b.route = "Line 1" b.driver = assign_driver("Fatmir")
At the end of execution of the above script, what is value of?
b.route: | |
b.driver.name: |
Exercises
class Hen: """Represents a hen""" class Robot: """Represents a robot""" def print_robot_hen(robot): s = "name: {}, pet's name: {}\n{}'s pet's favorite food is {}." print(s.format(robot.name, robot.pet.name, robot.name, robot.pet.fav_food))
Define a function, create_hen, which takes two strings and one integer, name, fav_food and age, creates a Hen object, assigns name to object's name attribute, fav_food to object's fav_food attribute and age to object's age attribute, and returns the object
Create a robot, robot4, and add name attribute to it, which is a string representing robot's name
Call create_hen, passing the required arguments, and assign it's return value to pet attribute of robot4
Call print_robot_hen, passing robot4 as argument
Create classes Car and Engine.
Create a car with a brand.
Write a function, build_engine(hp) that returns an Engine instance with such hp
Call build_engine, passing the required arguments, and assign it's return value to engine attribute of created car.
Create classes Family and Person.
Create a family.
Write a function, make_person(name) that returns a person with such name.
Create some people using make_person function, and add them to the created family's members list
Create classes Building and Door.
Create a building.
Write a function, create_door(material) that returns a door of such material.
Create some people using create_door function, and add them to the created building's doors list
class Laptop: pass class Charger: pass def make_charger(watts): c = Charger() c.watts = watts return c l = Laptop() l.brand = "HP" l.charger = make_charger(65)
At the end of execution of the above script, what is value of?
l.brand: | |
l.charger.watts: |