Last modified: Dec 01, 2026

Python Class Methods and Attributes

Classes are the building blocks of object-oriented programming in Python. They group data and behavior together.

This guide explains class methods and attributes clearly. You will see how they work, how they differ, and when to use each one.

What Is a Python Class?

A class is a blueprint for creating objects. Each object made from a class is called an instance.

Classes hold two main things: attributes (data) and methods (functions).


class Dog:
    species = "Canis familiaris"  # class attribute

    def __init__(self, name, age):
        self.name = name  # instance attribute
        self.age = age    # instance attribute

    def bark(self):       # instance method
        return f"{self.name} says woof!"

This small class already shows the key parts. Let's break them down one by one.

Instance Attributes

Instance attributes belong to a single object. They are usually set inside __init__.

Each instance has its own copy. Changing one does not affect the others.


dog1 = Dog("Rex", 3)
dog2 = Dog("Bella", 5)

print(dog1.name)  # Rex
print(dog2.name)  # Bella

Rex
Bella

Notice that dog1 and dog2 keep separate values. That is the core idea of instance attributes.

Class Attributes

Class attributes are shared by every instance. They are defined directly inside the class body.

Use them for values that stay the same across all objects, like constants or defaults.


print(Dog.species)    # Canis familiaris
print(dog1.species)   # Canis familiaris
print(dog2.species)   # Canis familiaris

Canis familiaris
Canis familiaris
Canis familiaris

Be careful when you change a class attribute through an instance. Python creates a new instance attribute instead of updating the shared value.

If you are unsure how data is stored, this guide on Python property vs attribute explains the difference in detail.

Instance Methods

An instance method takes self as its first parameter. It can read and change instance data.

The self keyword refers to the current object. If you come from Java, this comparison of Python self vs Java this will help.


class Counter:
    def __init__(self):
        self.count = 0

    def increment(self):
        self.count += 1
        return self.count

c = Counter()
print(c.increment())  # 1
print(c.increment())  # 2

1
2

Instance methods are the most common type. They work on a single object at a time.

Class Methods

A class method takes cls as its first parameter. It works with the class itself, not one instance.

You create one with the @classmethod decorator. Class methods are great for alternative constructors.


class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    @classmethod
    def from_string(cls, data):
        name, age = data.split(",")
        return cls(name, int(age))

p = Person.from_string("Alice,30")
print(p.name, p.age)  # Alice 30

Alice 30

Class methods do not need an instance. You can call them on the class directly.

For more patterns, see these Python class methods examples with real use cases.

Static Methods

A static method takes no automatic first argument. It is just a plain function placed inside a class.

Use @staticmethod when the logic belongs to the class but does not need class or instance data.


class MathHelper:
    @staticmethod
    def add(a, b):
        return a + b

print(MathHelper.add(2, 3))  # 5

5

Static methods keep related code organized without adding dependencies.

Properties: Controlled Attribute Access

Sometimes you want to control how an attribute is read or changed. Python properties let you do that cleanly.

A property looks like an attribute but runs a method behind the scenes. This guide on what does property do in Python covers the basics.


class Circle:
    def __init__(self, radius):
        self._radius = radius

    @property
    def radius(self):
        return self._radius

    @radius.setter
    def radius(self, value):
        if value < 0:
            raise ValueError("Radius cannot be negative")
        self._radius = value

c = Circle(5)
print(c.radius)  # 5
c.radius = 10
print(c.radius)  # 10

5
10

Properties add validation without changing how users access the attribute. That keeps your API simple and safe.

Public, Protected, and Private Attributes

Python does not enforce access control. It uses naming conventions instead.

A single underscore like _value means protected. A double underscore like __value triggers name mangling.


class Account:
    def __init__(self):
        self.balance = 100     # public
        self._owner = "Sam"    # protected
        self.__pin = 1234      # private (name-mangled)

acc = Account()
print(acc.balance)  # 100
print(acc._owner)   # Sam

100
Sam

These rules are conventions, not hard limits. They signal intent to other developers.

Class Attributes vs Instance Attributes

Choosing the right type matters. Class attributes save memory because they are shared.

Instance attributes give each object its own state. Use them for values that differ per object.

Mutable class attributes can cause bugs. A shared list will be modified by every instance.


class Bad:
    items = []  # shared by all instances

b1 = Bad()
b2 = Bad()
b1.items.append("x")
print(b2.items)  # ['x'] - surprise!

['x']

The fix is simple. Create mutable data inside __init__ so each instance gets its own copy.

Best Practices

Keep attributes minimal and meaningful. Initialize them in __init__.

Use class methods for factory patterns. Use static methods for utility helpers.

Reach for properties when you need validation or computed values. Avoid them for simple data.

Follow naming conventions. Use underscores to show access intent.

Write small methods with clear names. This makes your classes easy to read and test.

Conclusion

Python classes combine attributes and methods into clean, reusable units. Instance attributes hold per-object data, while class attributes are shared.

Instance methods work on objects. Class methods work on the class. Static methods are plain helpers.

Properties add safe, controlled access to attributes. Together, these tools let you build clear and reliable code.

Start with simple classes. Add properties and class methods only when you need them. Practice with small examples, and the patterns will become natural.