Modules -> OOP -> Classes and functions -> Instances as arguments

Instances as arguments


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

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

You can use attribute values as part of any expression. For example:

>>> f'Name: {dog.name}, Age: {dog.age}'
Name: Murki, Age: 2
>>> new_age = dog.age ** 3
>>> new_age
8

You can pass an instance as an argument in the usual way. For example:

...
def print_dog(d):
    print(f'Name: {d.name}, Age: {d.age}')

print_dog takes a dog as an argument and displays it. To invoke it, you can pass dog as an argument:

...
>>> print_point(dog)
Name: Murki, Age: 2
def calculate_dogs_birth_year(dog, current_year):
    return current_year - dog.age

calculate_dogs_birth_year takes a dog and current year, and returns the year the dog was born.

...
>>> calculate_dogs_birth_year(dog, 2025)
2023

Exercises


Add two attributes to cow1, name and age, where name is a string representing name of the cow, age is an integer representing age of the cow.


Define a function named print_cow that takes a Cow object and prints name and age of the cow as in the following example:

    name: Penelope, age: 7


Call print_cow passing cow1 as argument




Define a class, Car.


Create car1 with brand = "Toyota" and year = 2020.


Write a function, get_car_age that takes and a car, and returns its age. Example:

>>> get_car_age(2025, car1)
5




Define a class, Student.


Create a Student instance with one attribute, grade and assign an integer to it.


Write a function, passed_exam, that takes a Student instance, and returns True if its grade is greater than or equal to 6. Example:

>>> passed_exam(s1)
True




Define a class, Fish.


Create a Fish instance with one attribute, length and assign an integer to it.


Write a function, is_small, that takes a Fish instance, and returns True if its length is less than 10. Example:

>>> is_small(fish)
False



class Movie:
    """Represents a movie."""

movie1 = Movie()
movie1.title, movie1.year = "Inception", 2010

def movie_age(current_year, movie):
    return current_year - movie.year
    
result = movie_age(2025, movie1)

Given the above script, what is value of result?



Complete execution flow of the following program

Misses: 0
class Movie:
    "Represents a movie"

td = Movie()
td.title = "Training day"
td.duration = 7320

def print_movie(m):
    print("Title: {}, Duration: {}".format(m.title, m.duration))

print_movie(td)






Exercises


Add three attributes to hen1, name, age and fav_food, where name is a string representing name of the hen, age is an integer representing age of the hen, fav_food is a string representing hen's favorite food.


Define a function named print_hen that takes a Hen object and prints hen's name, age and favorite food as in the following example:

    name: Henny Penny, age: 1, favorite food: sunflower seeds


Call print_hen passing hen1 as argument




Define a class, Book.


Create book1 with title = "Think Python" and pages = 325.


Write a function, print_book, that takes a Book instance, and prints book details as in the below example:

Example:

>>> print_book(book)
title: Think Python, pages: 325




Define a class, Phone.


Create a Phone instance with three attributes: model: str, price: float, and storage: int.


Write a function, describe_phone that takes a Phone instance, and it prints the phone details as in the below example:

Example:

>>> describe_phone(phone1)
'iPhone with 128GB'




Define a class, City.


Create a city with two attributes: name: str, population: int.


Write a function, is_medium that takes a city, and returns True if its population is between 100000 and 200000 (including both), False otherwise. Example:

>>> is_medium(city)
False



class Laptop:
    """Represents a laptop."""

laptop1 = Laptop()
laptop1.brand, laptop1.ram = "HP", 8

def upgrade_ram(laptop):
    return laptop.ram * 2
    
result = upgrade_ram(laptop1)

Given the above script, what is value of result?



Complete execution flow of the following program

Misses: 0
class Movie:
    "Represents a movie"

td = Movie()
td.title = "Training day"
td.duration = 7320

def get_hours(m):
    return int(m.duration / 3600)

print("{} hours".format(get_hours(td)))