Skip to content

Classes and objects

Quality Score

Overall Score: 8.8/10 ✅ Excellent

  • Technical Accuracy: 32/35
  • Code Quality: 22/25
  • Educational Value: 21/25
  • Documentation: 13/15

Last reviewed: June 22, 2026

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

Represents a pizza with customizable ingredients and pricing.

This class demonstrates key object-oriented concepts including: - Class attributes (shared across all instances) - Instance attributes (unique to each instance) - Class methods for creating predefined pizza types - Static methods for utility functions - Instance methods for calculations

Attributes:

Name Type Description
price_per_ingredient float

Base price for each ingredient. This is a class attribute shared by all Pizza instances. Defaults to 3.

ingredients list[IngredientEnum]

List of ingredients in this specific pizza. This is an instance attribute unique to each Pizza.

Source code in src/beginner/classes_and_objects/classes_and_objects.py
 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
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
class Pizza:
    """Represents a pizza with customizable ingredients and pricing.

    This class demonstrates key object-oriented concepts including:
    - Class attributes (shared across all instances)
    - Instance attributes (unique to each instance)
    - Class methods for creating predefined pizza types
    - Static methods for utility functions
    - Instance methods for calculations

    Attributes:
        price_per_ingredient: Base price for each ingredient. This is a
            class attribute shared by all Pizza instances. Defaults to 3.
        ingredients: List of ingredients in this specific pizza. This is an
            instance attribute unique to each Pizza.
    """

    price_per_ingredient: float = 3

    def __init__(self, ingredients: list[IngredientEnum]) -> None:
        """Initialize a Pizza with a list of ingredients.

        Args:
            ingredients: List of ingredients to include in the pizza.
                Each ingredient must be from the IngredientEnum.
        """
        self.ingredients: list[IngredientEnum] = ingredients

    @classmethod
    def napolitana(cls) -> "Pizza":
        """Create a Napolitana (Margherita) pizza with traditional ingredients.

        This class method demonstrates the factory pattern for creating
        specific pizza types. All Napolitana pizzas have the same standard
        ingredients: basil, tomato, and mozzarella.

        Returns:
            A new Pizza instance with Napolitana ingredients.
        """
        ingredients = [
            IngredientEnum.BASIL,
            IngredientEnum.TOMATO,
            IngredientEnum.MOZZARELLA,
        ]
        return cls(ingredients=ingredients)

    @classmethod
    def four_cheese(cls) -> "Pizza":
        """Create a Four Cheese pizza.

        This class method demonstrates the factory pattern for creating
        specific pizza types. All Four Cheese pizzas have the same standard
        ingredients: gorgonzola, mozzarella, emmental, and parmesan.

        Returns:
            A new Pizza instance with Four Cheese ingredients.
        """
        ingredients = [
            IngredientEnum.GORGONZOLA,
            IngredientEnum.MOZZARELLA,
            IngredientEnum.EMMENTAL,
            IngredientEnum.PARMESAN,
        ]
        return cls(ingredients=ingredients)

    def price(self) -> float:
        """Calculate the total price of this pizza.

        The price is calculated by multiplying the number of ingredients
        by the price_per_ingredient class attribute. This demonstrates
        how instance methods can access both instance and class attributes.

        Returns:
            The total price for this pizza based on its ingredients.
        """
        return len(self.ingredients) * self.price_per_ingredient

    @staticmethod
    def list_all_ingredients() -> list[IngredientEnum]:
        """List all available ingredients for creating pizzas.

        This static method demonstrates a utility function that doesn't
        need access to instance or class attributes. It provides a convenient
        way to retrieve all possible ingredients from the IngredientEnum.

        Returns:
            A list of all IngredientEnum values representing available
            ingredients.
        """
        return [ingredient for ingredient in IngredientEnum]
__init__(ingredients)

Initialize a Pizza with a list of ingredients.

Parameters:

Name Type Description Default
ingredients list[IngredientEnum]

List of ingredients to include in the pizza. Each ingredient must be from the IngredientEnum.

required
Source code in src/beginner/classes_and_objects/classes_and_objects.py
49
50
51
52
53
54
55
56
def __init__(self, ingredients: list[IngredientEnum]) -> None:
    """Initialize a Pizza with a list of ingredients.

    Args:
        ingredients: List of ingredients to include in the pizza.
            Each ingredient must be from the IngredientEnum.
    """
    self.ingredients: list[IngredientEnum] = ingredients
four_cheese() classmethod

Create a Four Cheese pizza.

This class method demonstrates the factory pattern for creating specific pizza types. All Four Cheese pizzas have the same standard ingredients: gorgonzola, mozzarella, emmental, and parmesan.

Returns:

Type Description
Pizza

A new Pizza instance with Four Cheese ingredients.

Source code in src/beginner/classes_and_objects/classes_and_objects.py
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
@classmethod
def four_cheese(cls) -> "Pizza":
    """Create a Four Cheese pizza.

    This class method demonstrates the factory pattern for creating
    specific pizza types. All Four Cheese pizzas have the same standard
    ingredients: gorgonzola, mozzarella, emmental, and parmesan.

    Returns:
        A new Pizza instance with Four Cheese ingredients.
    """
    ingredients = [
        IngredientEnum.GORGONZOLA,
        IngredientEnum.MOZZARELLA,
        IngredientEnum.EMMENTAL,
        IngredientEnum.PARMESAN,
    ]
    return cls(ingredients=ingredients)
list_all_ingredients() staticmethod

List all available ingredients for creating pizzas.

This static method demonstrates a utility function that doesn't need access to instance or class attributes. It provides a convenient way to retrieve all possible ingredients from the IngredientEnum.

Returns:

Type Description
list[IngredientEnum]

A list of all IngredientEnum values representing available

list[IngredientEnum]

ingredients.

Source code in src/beginner/classes_and_objects/classes_and_objects.py
107
108
109
110
111
112
113
114
115
116
117
118
119
@staticmethod
def list_all_ingredients() -> list[IngredientEnum]:
    """List all available ingredients for creating pizzas.

    This static method demonstrates a utility function that doesn't
    need access to instance or class attributes. It provides a convenient
    way to retrieve all possible ingredients from the IngredientEnum.

    Returns:
        A list of all IngredientEnum values representing available
        ingredients.
    """
    return [ingredient for ingredient in IngredientEnum]
napolitana() classmethod

Create a Napolitana (Margherita) pizza with traditional ingredients.

This class method demonstrates the factory pattern for creating specific pizza types. All Napolitana pizzas have the same standard ingredients: basil, tomato, and mozzarella.

Returns:

Type Description
Pizza

A new Pizza instance with Napolitana ingredients.

Source code in src/beginner/classes_and_objects/classes_and_objects.py
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
@classmethod
def napolitana(cls) -> "Pizza":
    """Create a Napolitana (Margherita) pizza with traditional ingredients.

    This class method demonstrates the factory pattern for creating
    specific pizza types. All Napolitana pizzas have the same standard
    ingredients: basil, tomato, and mozzarella.

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

Calculate the total price of this pizza.

The price is calculated by multiplying the number of ingredients by the price_per_ingredient class attribute. This demonstrates how instance methods can access both instance and class attributes.

Returns:

Type Description
float

The total price for this pizza based on its ingredients.

Source code in src/beginner/classes_and_objects/classes_and_objects.py
 95
 96
 97
 98
 99
100
101
102
103
104
105
def price(self) -> float:
    """Calculate the total price of this pizza.

    The price is calculated by multiplying the number of ingredients
    by the price_per_ingredient class attribute. This demonstrates
    how instance methods can access both instance and class attributes.

    Returns:
        The total price for this pizza based on its ingredients.
    """
    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.

Common pitfalls

Do not use mutable objects as default values for the class creation or arguments. They are shared across all instances of the class, and this is usually something with unexpected effects, and it is hard to debug.

# bad example
class Pizza:
    def __init__(self, toppings=[]):
        self.toppings = toppings

# correct example
class Pizza:
    def __init__(self, toppings=None):
        if toppings is None:
            toppings = []
        self.toppings = toppings