Modules -> OOP -> Methods -> Naming methods

Naming methods


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(self, name, age, energy):
        self.name = name
        self.age = age
        self.energy = energy

murki = Dog()
murki.set_dog_info("Murki", 2, 50)

When print_dog was a standalone function, it made sense to include dog in the name to indicate what it was printing. However, now that the method is part of the Dog class, and we call it using a Dog instance (like murki.print()), it's already clear that we are dealing with a dog. Therefore, we can simplify the method name to just print. Since it's called on a dog instance, murki.print() already tells us we are printing information about the murki, the dog.

class Dog:
    ...

    def print(d):
        print(f'Name: {d.name}, Age: {d.age}')
>>> murki.print()
Name: Murki, Age: 2

Let's do the same for set_dog_info:

class Dog:
    """Represents a dog.

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

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

    def set_info(self, name, age, energy):
        self.name = name
        self.age = age
        self.energy = energy
>>> murki.set_info("Murki", 2, 50)
>>> murki.print()
Name: Murki, Age: 2

Exercises


Define a class, Bird with attributes: species: str, wingspan: float, can_fly: bool.


Write a method, initialize(self, species: str, wingspan: float, can_fly: bool), that sets all three attributes.


Write a method, describe, that prints the bird's info as in the following example:

    Species: Eagle, Can fly: True


Create a bird and call both methods.



Define a class, FootballPlayer with attributes: name: str, position: str, goals: int.


Create a method, load_player(self, name: str, position: str, goals: int) that sets the attributes.


Write a method, show_stats, that prints the player information as in the following example:

    Aliaj scored 25 goals 


Create a football player and call its methods.



Define a class, Painting, with attributes: title: str, artist: str, year: int.


Create a method, add_painting(self, title: str, artist: str, year: int) that sets the attributes.


Write a method, print, that prints the painting information as in the following example:

    Mona Lisa by Leonardo da Vinci


Create a painting and call its methods.



Define a class, Movie, with attributes: title: str, duration: int, rating: float.


Create a method, setup(self, title: str, duration: int, rating: float), that sets the attributes.


Write a method, info, that prints the movie information as in the following example:

    Inception has a rating of 8.8


Create a movie and call its methods.



Define a class, Parent, with attributes: first_name: str, children_count: int.


Create a method, update_info(self, first_name: str, children_count: int), that sets the attributes.


Write a method, print_children, that prints the parent information as in the following example:

    Aferdita has 3 children


Create a parent and call its methods.



Exercises


Create a class, Laptop, with attributes: brand: str, ram: int, storage: int.


Define a method, specs(self, brand: str, ram: int, storage: int) that sets the attributes.


Define a method, display_specs that prints the laptop information as in the following example:

    RAM: 16GB, Storage: 512GB


Create a laptop object and call both methods.



Define a class, Recipe, with attributes: title: str, ingredients: list, prep_time: int.


Write a method, add_details(self, title: str, ingredients: list[str], prep_time: int), that sets all attributes.


Write a method, summary, that prints the recipe as in the following example:

    Recipe: Pancakes (20 min)


Create a recipe and call both methods.



Define a class, Tradition, with attributes: name: str, origin: str, is_religious: bool.


Create a method, define(self, name: str, origin: str, is_religious: bool) that sets the attributes.


Write a method, describe, that prints the tradition information as in the following example:

    Diwali comes from India


Create a tradition and call its methods.



Define a class, Song, with attributes: title: str, artist: str, length: float.


Create a method, load_song(self, title: str, artist: str, length: float) that sets the attributes.


Write a method, preview, that prints the song information as in the following example:

    Bohemian Rhapsody by Queen


Create a song and call its methods.



Define a class, Course, with attributes: code: str, credits: int, instructor: str.


Create a method, assign(self, code: str, credits: int, instructor: str), that sets the attributes.


Write a method, info, that prints the course information as in the following example:

    CS101 has 4 credits


Create a course and call its methods.