Modules -> OOP -> Methods -> Modifying objects

Modifying objects


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

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

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


def set_dog_info(dog, name, age, energy):
    dog.name = name
    dog.age = age
    dog.energy = energy

dog = Dog()

We have:

  • A class, Dog, with one method, print_dog.
  • A function, set_dog_info.

We already mentioned the convention of python to name the first parameter of each method self, and is meant to work with an object (an instance of the class).

The set_dog_info function takes a Dog instance as its first argument, so it makes sense to turn it into a method to explicitly associate it with the Dog class.

class Dog:
    ...
    def set_dog_info(self, name, age, energy):
        self.name = name
        self.age = age
        self.energy = energy

...

We moved set_dog_info four spaces to the right to place it inside the Dog class, renamed its first parameter to self, and updated all its references within the method.

dog.print_dog() called print method passing dog as the first argument. This because print method has only one parameter, self.

Different from print, set_dog_info has three more parameters, name, age and energy, and if we try to call it in the same way:

>>> dog.set_dog_info()
TypeError("Dog.set_dog_info() missing 3 required positional arguments: 'name', 'age', and 'energy'")
>>>

we get an error saying that we have to provide arguments for three extra parameters, name, age and energy.

Extra arguments, beside self, are passed inside the parenthesis in the method call:

>>> dog.set_dog_info("Llesi", 3, 50)

So:

  • dog is assigned to self.
  • "Llesi" is assigned to name.
  • 3 is assigned to age.
  • 100 is assigned to energy.
...
>>> dog.print_dog()
Name: Llesi, Age: 3

We don't care what the outside calls the dog, inside the class, we always refer to it as self.

Exercises

class Cat:
    """Represents a cat.

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

    def print_info(self):
        print(f'Name: {self.name}, Age: {self.age}, Color: {self.color}')


Write a method named set, which takes one string and two integers as arguments, name, age, and color. The method should set three attributes on the object: name, age, and color, and assign the corresponding arguments to them.


Create a cat and call the set method.


class Student:
    """Represents a student.

    attributes: name: str, grade: int, school: str
    """

    def print_detail(self):
        print(f'Name: {self.name}, School: {self.school}')


Write a method, set_values, that takes a string, name, an integer, grade, and a string, school. The method should assign those arguments to the respective attributes name, grade, and school.


Create a student and call set_values.


class Book:
    """Represents a book.

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

    def print_book(self):
        print(f'Title: {self.title}, Pages: {self.pages}')


Write a method, set(title: str, pages: int), that sets object's title and pages attributes.


Create a book and set its attributes.


class House:
    """Represents a house.

    attributes: address: str, rooms: int, area: int
    """

    def show_info(self):
        print(f'Address: {self.address}, Area: {self.area} m square')


Write a method that sets all three attributes.


Create a house and call the method.


class Laptop:
    """Represents a laptop.

    attributes: brand: str, ram: int, storage: int
    """

    def print_specs(self):
        print(f'Brand: {self.brand}, RAM: {self.ram}GB, Storage: {self.storage}GB')


Write method, configure, to set brand, ram, and storage.


Create a laptop and call configure.



Exercises

class Car:
    """Represents a car.

    attributes: model: str, year: int
    """

    def display(self):
        print(f'Model: {self.model}, Year: {self.year}')


Write a method, assign, which takes a string and an integer, model and year. The method should assign these to the object's model and year attributes.


Create a car and call the assign method.


class Phone:
    """Represents a phone.

    attributes: brand: str, price: int
    """

    def print_info(self):
        print(f'Brand: {self.brand}, Price: ${self.price}')


Write a method, setup, that takes two arguments, a string, brand, and an integer, price. The method should assign them to object's brand and price attributes.


Create a phone and call setup.


class Movie:
    """Represents a movie.

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

    def display(self):
        print(f'Title: {self.title}, Year: {self.year}, Genre: {self.genre}')


Write a method, set_movie(title: str, year: int, genre: str), which sets objects title, year, and genre attributes.


Create a movie and set its attributes.


class Player:
    """Represents a sports player.

    attributes: name: str, sport: str
    """

    def print_player(self):
        print(f'Name: {self.name}, Sport: {self.sport}')


Write a method to assign values to name and sport.


Create a player and assign values.


class Country:
    """Represents a country.

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

    def show(self):
        print(f'Country: {self.name}, Population: {self.population}')


Write method, set_country to set name and population.


Create a country and call set_country.