Modules -> OOP -> Methods -> Printing objects

Printing objects


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

    attributes: name: str, age: int
    """

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

To call print_dog, we have to pass a Dog instance as an argument:

>>> dog = Dog()
>>> dog.name, dog.age = "Murki", 2
>>> print_dog(dog)
Name: Murki, Age: 2

To make print_dog a method, all we have to do is move the function definition inside the class definition. Notice the change in indentation.

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

Now print_dog is a Dog method.

So, a method is a function that is defined inside a class and is meant to work with an object (an instance of the class).

There are two ways to call print_dog. The first (and less common) way is to use function syntax:

>>> Dog.print_dog(dog)
Name: Murki, Age: 2

In this use of dot notation, Dog is the name of the class, and print_dog is the name of the method. dog is passed as a parameter.

The second (and more concise) way is to use method syntax:

>>> dog.print_dog()
Name: Murki, Age: 2

In this use of dot notation, print_dog is the name of the method (again), and dog is the object the method is invoked on, which is called the subject. Just as the subject of a sentence is what the sentence is about, the subject of a method invocation is what the method is about.

Inside the method, the subject is assigned to the first parameter, so in this case dog is assigned to d.

By convention, the first parameter of a method is called self, so it would be more common to write print_dog like this:

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

The reason for this convention is an implicit metaphor:

  • The syntax for a function call, print_dog(dog), suggests that the function is the active agent. It says something like, "Hey print_dog! Here's an object for you to print."
  • In object-oriented programming, the objects are the active agents. A method invocation like dog.print_dog() says "Hey dog! Please print yourself."

Exercises

class Cat:
    """Represents a cat.

    attributes: name: str, age: int
    """


Write a Cat method, print_cat, that prints cat's information in the following format:

    name - age
Example:

    Nina - 3


Create a cat, set its attributes and call print_cat


class Laptop:
    """Represents a laptop.

    attributes: brand: str, ram: int (in GB)
    """


Write a Laptop method, print_info, that prints laptop's information in the following format:

    brand - RAMGB
Example:

    Dell - 16GB


Create a laptop, set its attributes and call print_info


class Movie:
    """Represents a movie.

    attributes: title: str, year: int
    """


Write a Movie method, print, that prints movie's information in the following format:

    title (year)
Example:

    Inception (2010)


Create a movie, set its attributes and call print


class Book:
    """Represents a book.

    attributes: title: str, author: str, pages: int
    """


Write a Book method, print_book, that prints book's information in the following format:

    Title: book title | Author: book author | Pages: number of pages
Example:

    Title: Siddhartha | Author: Hermann Hesse | Pages: 160


Create a book, set its attributes and call print_book


class Dog:
    """Represents a dog.

    attributes: name: str, weight: float (in kg)
    """


Write a Dog method, print, that prints dog's information in the following format:

    name - weightkg
Example:

    Bubi - 12.5kg


Create a dog, set its attributes and call print



Exercises

class Student:
    """Represents a student.

    attributes: first_name: str, last_name: str, grade: float
    """


Write a Student method, print_student, that prints student's information in the following format:

    full name - grade
Example:

    Sara Aliu - 8.6


Create a student, set its attributes and call print_student


class FootballPlayer:
    """Represents a football player.

    attributes: name: str, goals: int
    """


Write a FootballPlayer method, print_detail, that prints player's information in the following format:

    name - goals goals
Example:

    Messi - 38 goals


Create a football player, set its attributes and call print_detail


class Car:
    """Represents a car.

    attributes: brand: str, mileage: int (in km)
    """


Write a Car method, display, that prints car's information in the following format:

    brand - mileage km
Example:

    Toyota - 55000 km


Create a car, set its attributes and call display


class City:
    """Represents a city.

    attributes: name: str, population: int, country: str
    """


Write a City method, print_city, that prints city's information in the following format:

    Name: city's name, Country: city's country | population people
Example:

    Name: Tirane, Country: Albania | 600000 people


Create a city, set its attributes and call print_city


class Phone:
    """Represents a phone.

    attributes: model: str, price: float
    """


Write a Phone method, print_details, that prints phone's information in the following format:

    model - $price
Example:

    iPhone 13 - $999.99


Create a phone, set its attributes and call print_details