On this page

Attributes & Methods

Enrico Hochkoeppler Profile

By Enrico Hochkoeppler

Web Developer & TutoriaLibre Founder

How Objects Store Data: Attributes​

Attributes are named pieces of data stored inside an object. Each object maintains its own independent set of attributes, allowing different instances of the same class to hold different values.

Attributes are typically defined inside the __init__ method:


class Person:

    def __init__(self, name, age):

        self.name = name      # instance attribute

        self.age = age        # instance attribute

Here:

Example usage:


p1 = Person("Alice", 30)

p2 = Person("Bob", 25)

p1 and p2 now contain different attribute values.

Accessing and Modifying Attributes​

Attributes are accessed using dot notation:


print(p1.name)

print(p2.age)

They can be modified at any time:


p1.age = 31

Python allows adding new attributes dynamically, but in well‑structured programs, attributes should be defined inside __init__ to maintain clarity and consistency.

How Objects Behave: Methods​

Methods are functions defined inside a class that operate on the object’s data. They always take self as their first parameter, which refers to the instance calling the method.

Example:


class Person:

    def __init__(self, name):

        self.name = name



    def greet(self):

        return f"Hello, I am {self.name}."

Calling a method:


p = Person("Alice")

p.greet()

The method uses self.name to access the object’s internal state.

Why self Matters​

self is the mechanism that connects a method to the specific object instance. It allows each object to:

Without self, methods would not know which object’s data they should operate on.

Combining Attributes and Methods​

Attributes store state, and methods define behavior. Together, they form the core of object‑oriented programming.

Example:


class BankAccount:

    def __init__(self, owner, balance=0):

        self.owner = owner

        self.balance = balance



    def deposit(self, amount):

        self.balance += amount

        return self.balance



    def withdraw(self, amount):

        if amount <= self.balance:

            self.balance -= amount

            return self.balance

        return "Insufficient funds"

This class models a real‑world concept:

Each object represents a separate bank account with its own data and behavior.

Method Types: Instance, Class, and Static Methods​

Although most methods are instance methods, Python supports two additional method types.

Instance Methods​

The most common type. They operate on object data and require self.


def method(self):

    ...

Class Methods​

Operate on the class itself rather than individual objects. They use @classmethod and receive cls instead of self.


@classmethod

def create_empty(cls):

    return cls("", 0)

Static Methods​

Utility functions placed inside a class for organizational purposes. They do not access self or cls.


@staticmethod

def is_valid_amount(value):

    return value >= 0

These method types support different design needs and help structure complex classes.

Best Practices for Attributes and Methods​

These practices lead to clean, predictable, and maintainable object‑oriented code.

Summary​

In this lesson, you learned how attributes store object data and how methods define object behavior. You explored the role of self, how to access and modify attributes, how to design meaningful methods, and how Python supports instance, class, and static methods. These concepts form the foundation for building robust, well‑structured classes in Python.

Found this helpful? Share the knowledge...

... or support this project to keep it alive!

Buy Me a Coffee

Previous

Classes & Objects

Next

Inheritance