Skip to content

Classes and objects

Class

A class is the common structure for all objects or instances.

A class can have different methods and attributes:

  • Static method: method that is bound to the class and not the object of the class.
  • Class method: takes cls as the first parameter. It can modify a class state that would apply across all the instances of the class.
  • Class attributes: Attributes that are common for all instances of the class. Careful, class attributes are mutable, that means, if value changes, it affects to all classes and objects. Use mainly for constants/default values and tracing data across all classes.

src.beginner.classes_and_objects

Module to set an instance of a Pizza with Ingredients.

This module contains the enum for all ingredients available to create a Pizza, and the class to create a Pizza instance.

src.beginner.classes_and_objects.Pizza

Class to represent a Pizza.

Attributes:

Name Type Description
price_per_ingredient float

price per ingredient. Defaults to 3.

Source code in src/beginner/classes_and_objects/classes_and_objects.py
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
class Pizza:
    """Class to represent a Pizza.

    Attributes:
        price_per_ingredient (float): price per ingredient. Defaults to 3.
    """

    price_per_ingredient: float = 3

    def __init__(self, ingredients: list[IngredientEnum]) -> None:
        """Constructor for Pizza class.

        Args:
            ingredients (list[IngredientEnum]): List of ingredients for a Pizza
        """
        self.ingredients: list[IngredientEnum] = ingredients

    @classmethod
    def napolitana(cls) -> "Pizza":
        """Class Method to create a Napolitana Pizza instance.

        We can create a classmethod, as all napolitana pizzas
        will have the same ingredients.

        Returns:
            Pizza: a Pizza instance.
        """
        ingredients = [
            IngredientEnum.BASIL,
            IngredientEnum.TOMATO,
            IngredientEnum.MOZZARELLA,
        ]
        return cls(ingredients=ingredients)

    @classmethod
    def four_cheese(cls) -> "Pizza":
        """Class Method to create a Four cheese Pizza instance.

        We can create a classmethod, as all four cheese pizzas
        will have the same ingredients.

        Returns:
            Pizza: a Pizza instance.
        """
        ingredients = [
            IngredientEnum.GORGONZOLA,
            IngredientEnum.MOZZARELLA,
            IngredientEnum.EMMENTAL,
            IngredientEnum.PARMESAN,
        ]
        return cls(ingredients=ingredients)

    def price(self) -> float:
        """Method to get price for Pizza instance.

        It depends on the number of ingredients a Pizza has,
        and the price per ingredient.

        Returns:
            float: total price for a Pizza instance.
        """
        return len(self.ingredients) * self.price_per_ingredient

    @staticmethod
    def list_all_ingredients() -> list[IngredientEnum]:
        """Static method to list all ingredients available to create a Pizza.

        It can be a static method, as there is no relation with Pizza instance.

        Returns:
            list[IngredientEnum]: list of ingredients to create a Pizza.
        """
        return [ingredient for ingredient in IngredientEnum]
__init__(ingredients)

Constructor for Pizza class.

Parameters:

Name Type Description Default
ingredients list[IngredientEnum]

List of ingredients for a Pizza

required
Source code in src/beginner/classes_and_objects/classes_and_objects.py
34
35
36
37
38
39
40
def __init__(self, ingredients: list[IngredientEnum]) -> None:
    """Constructor for Pizza class.

    Args:
        ingredients (list[IngredientEnum]): List of ingredients for a Pizza
    """
    self.ingredients: list[IngredientEnum] = ingredients
four_cheese() classmethod

Class Method to create a Four cheese Pizza instance.

We can create a classmethod, as all four cheese pizzas will have the same ingredients.

Returns:

Name Type Description
Pizza Pizza

a Pizza instance.

Source code in src/beginner/classes_and_objects/classes_and_objects.py
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
@classmethod
def four_cheese(cls) -> "Pizza":
    """Class Method to create a Four cheese Pizza instance.

    We can create a classmethod, as all four cheese pizzas
    will have the same ingredients.

    Returns:
        Pizza: a Pizza instance.
    """
    ingredients = [
        IngredientEnum.GORGONZOLA,
        IngredientEnum.MOZZARELLA,
        IngredientEnum.EMMENTAL,
        IngredientEnum.PARMESAN,
    ]
    return cls(ingredients=ingredients)
list_all_ingredients() staticmethod

Static method to list all ingredients available to create a Pizza.

It can be a static method, as there is no relation with Pizza instance.

Returns:

Type Description
list[IngredientEnum]

list[IngredientEnum]: list of ingredients to create a Pizza.

Source code in src/beginner/classes_and_objects/classes_and_objects.py
88
89
90
91
92
93
94
95
96
97
@staticmethod
def list_all_ingredients() -> list[IngredientEnum]:
    """Static method to list all ingredients available to create a Pizza.

    It can be a static method, as there is no relation with Pizza instance.

    Returns:
        list[IngredientEnum]: list of ingredients to create a Pizza.
    """
    return [ingredient for ingredient in IngredientEnum]
napolitana() classmethod

Class Method to create a Napolitana Pizza instance.

We can create a classmethod, as all napolitana pizzas will have the same ingredients.

Returns:

Name Type Description
Pizza Pizza

a Pizza instance.

Source code in src/beginner/classes_and_objects/classes_and_objects.py
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
@classmethod
def napolitana(cls) -> "Pizza":
    """Class Method to create a Napolitana Pizza instance.

    We can create a classmethod, as all napolitana pizzas
    will have the same ingredients.

    Returns:
        Pizza: a Pizza instance.
    """
    ingredients = [
        IngredientEnum.BASIL,
        IngredientEnum.TOMATO,
        IngredientEnum.MOZZARELLA,
    ]
    return cls(ingredients=ingredients)
price()

Method to get price for Pizza instance.

It depends on the number of ingredients a Pizza has, and the price per ingredient.

Returns:

Name Type Description
float float

total price for a Pizza instance.

Source code in src/beginner/classes_and_objects/classes_and_objects.py
77
78
79
80
81
82
83
84
85
86
def price(self) -> float:
    """Method to get price for Pizza instance.

    It depends on the number of ingredients a Pizza has,
    and the price per ingredient.

    Returns:
        float: total price for a Pizza instance.
    """
    return len(self.ingredients) * self.price_per_ingredient

Object

An object is an instance of a class. It is a concrete entity based on arguments during creation. Two objects of the same class are different, with or without the same values.