Modules -> OOP -> Classes and functions -> Modifying instances as attribute values

Modifying instances as attribute values


Related code
class Dog:
    """Represents a dog.

    attributes: name, age and collar
    """

class Collar:
    """Represents a collar.

    attributes: color and size
    """

strap = Collar()
strap.color, strap.size = 'blue', 4

dog = Dog()
dog.name, dog.age, collar = "Murki", 2, strap

...
def update_dog_info(dog, dage, collar_dsize):
    dog.age += dage
    dog.collar.size += collar_dsize

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}")

update_dog_info takes a dog, dage and collar_dsize, and updates dog's age and dog's collar size accordingly.

print_dog_info takes a dog, and prints its info.

>>> update_dog_info(dog, 1, 2)
>>> print_dog_info(dog)
Name: Murki, age: 3
Murki's collar color: blue, Murki's collar size: 6
>>>

Exercises

class Cow:
    """Represents a cow"""

cow1 = Cow()

cow1.name, cow1.age = "Rose", 3


Create a class Robot and one instance of it, robot1


Add an attribute to robot1, name, representing robot's name, and an attribute pet, representing robot's pet and assign cow1 to this attribute


Write a function, print_robot_cow, which takes a Robot instance whose pet is a cow, and prints details about the robot as follows:

  name: Nick, pet's name: Rose
  Nick's pet is 3 years old


Call print_robot_cow, passing robot1 as its argument




Create two classes, Backpack and Student.


Write a function, set_backpack(bp: Backpack, color: str, books: int), that sets the color and books attributes of bp.


Write a function, set_student(st: Student, name: str, grade: int, backpack: Backpack), that sets the name, grade and backpack attributes of st.


Write a function, print_student(st: Student), that prints the student's details in the following format:

Name: Elira, Grade: 8
Backpack color: red, books: 3


Call set_backpack, set_student and print_student




Create two classes, Teacher and Laptop.


Write a function, set_laptop(l: Laptop, brand: str, ram: int), that sets the brand and ram attributes of l.


Write a function, set_teacher(t: Teacher, name: str, subject: str, laptop: Laptop), that sets the name, subject and laptop attributes of t.


Write a function, print_teacher(t: Teacher), that prints the teacher's details in the following format:

Name: Nora, Subject: Math
Laptop brand: Dell, RAM: 8GB


Call set_laptop, set_teacher, and print_teacher.




Create two classes, Athlete and Shoe.


Write a function, set_shoe(s: Shoe, brand: str, size: int), that sets the brand and size attributes of s.


Write a function, set_athlete(a: Athlete, name: str, sport: str, shoe: Shoe), that sets the name, sport and shoe attributes of a.


Write a function, print_athlete(a: Athlete), that prints the athlete's details in the following format:

Name: Leart, Sport: Basketball Shoe brand: Nike, size: 44


Call set_shoe, set_athlete, and print_athlete.



class Bike:
    """Represents a bike. Attributes: brand, gears, bell"""

class Bell:
    """Represents a bell. Attributes: tone, is_loud"""

def set_bell(b: Bell, tone: str, is_loud: bool):
    b.tone = tone
    b.is_loud = is_loud

def set_bike(b: Bike, brand: str, gears: int, bell: Bell):
    b.brand = brand
    b.gears = gears
    b.bell = bell

b, bell = Bike(), Bell()
set_bell(bell, "ring", True)
set_bike(b, "Trek", 21, bell)

At the end of execution of the above script, what is value of?

b.gears:
b.bell.is_loud:



Exercises

class Hen:
    """Represents a hen"""

hen1 = Hen()

hen1.name, hen1.age, hen1.fav_food = "Buck Beak", 3, "sunflower seeds"


Create a class Robot and one instance of it, robot2


Add an attribute to robot2, name, representing robot's name, and an attribute pet, representing robot's pet and assign hen1 to this attribute


Write a function, print_robot_hen, which takes a Robot instance whose pet is a hen, and prints details about the robot as follows:

  name: Mic, pet's name: Buck Beak
  Mic's pet's favorite food is sunflower seeds


Call print_robot_hen, passing robot2 as its argument




Create two classes, Cover and Book.


Write a function set_cover(cov: Cover, color: str, material: str) that sets the color and material of cov.


Write a function set_book(b: Book, title: str, pages: int, cover: Cover) that sets the pages and cover attributes of b.


Write a function, print_book(b: Book) that prints the book details as follows:

Title: Python Basics, Pages: 250
Cover color: blue, material: leather


Call set_cover, set_book and print_book




Create two classes, Painter and Brush.


Write a function set_brush(b: Brush, size: int, type: str) that sets the size and type of b.


Write a function set_painter(p: Painter, name: str, style: str, brush: Brush) that sets the name, style and brush attributes of p.


Write a function, print_painter(p: Painter) that prints the painter's details as follows:

Name: Lora, Style: Abstract
Brush size: 5, type: round


Call set_brush, set_painter and print_painter




Create two classes, Tree and Leaf.


Write a function set_leaf(l: Leaf, shape: str, length: int) that sets the shape and length of l.


Write a function set_tree(t: Tree, species: str, height: float, leaf: Leaf) that sets the species, height and leaf attributes of p.


Write a function, print_tree(t: Tree) that prints the tree's details as follows:

Species: Oak, Height: 5.5m
Leaf shape: oval, length: 12cm


Call set_leaf, set_tree and print_tree



class House:
    """Represents a house. Attributes: address, floors, window"""

class Window:
    """Represents a window. Attributes: width, is_open"""

def set_window(w: Window, width: int, is_open: bool):
    w.width = width
    w.is_open = is_open

def set_house(h: House, address: str, floors: int, window: Window):
    h.address = address
    h.floors = floors
    h.window = window

h, w = House(), Window()
set_window(w, 120, True)
set_house(h, "Elm Street 12", 2, w)

At the end of execution of the above script, what is value of?

h.floors:
h.window.width: