Python Classes for Beginners: __init__, self, Methods, Inheritance

Last edited Oct 16, 2026 · 4 min read

A class is a blueprint that bundles data and the functions that work on that data. The official Python tutorial puts it this way: classes bundle data and functionality together, and creating a class creates a new type of object, from which you can make as many instances as you like. This page builds one small class, a bank account, and shows what __init__, self, class variables and inheritance do. Every example was run in Python 3.13 and the output shown is what it printed. The tutorial text was read in October 2026.

1. A first class

class Account:
    def __init__(self, owner, balance=0):
        self.owner = owner
        self.balance = balance

    def deposit(self, amount):
        self.balance += amount

    def withdraw(self, amount):
        if amount > self.balance:
            print("Not enough funds")
            return
        self.balance -= amount

    def summary(self):
        return f"{self.owner}: {self.balance}"

Nothing happens when this code runs, other than Python learning that Account exists. As the tutorial says, a class definition has to be executed before it has any effect. To get an object, call the class like a function:

a = Account("Mia", 100)
b = Account("Leo")
a.deposit(50)
a.withdraw(500)
a.withdraw(30)
print(a.summary())
print(b.summary())
print(a.balance, b.balance)
Not enough funds
Mia: 120
Leo: 0
120 0

a and b are two separate instances. Each has its own owner and balance, so changing Mia's balance does not touch Leo's. The withdrawal of 500 was refused, and only the 30 went through: 100 + 50 - 30 = 120.

2. What init does

When you write Account("Mia", 100), Python creates a new empty object and then calls __init__ on it, passing along your arguments. The tutorial describes __init__ as the special method that runs automatically when an instance is created, and says arguments given to the class are passed to it. Inside it, self.owner = owner attaches a piece of data to this particular object. That is why Leo, created with no second argument, got the default balance of 0.

3. What self is

Look at a.deposit(50). The method is defined with two parameters, self and amount, yet you passed one value. The tutorial explains that a method is declared with an explicit first argument representing the object, and that the call supplies it implicitly. So a.deposit(50) behaves like Account.deposit(a, 50). By convention the first parameter is named self.

Forgetting it is a common beginner error:

class Oops:
    def hello():
        return "hi"

Oops().hello()
TypeError: Oops.hello() takes 0 positional arguments but 1 was given

Python passed the object in automatically, and the method had no parameter to receive it.

4. Class variables and instance variables

The tutorial separates two kinds of data. Instance variables, set through self, are unique to each object. Class variables, defined in the class body, are shared by all instances. A class variable fits something like a counter of how many objects exist:

class Counter:
    created = 0
    def __init__(self):
        Counter.created += 1

x = Counter()
y = Counter()
print(Counter.created, x.created, y.created)
2 2 2

All three names show the same shared value.

5. The shared-list trap

The tutorial warns that shared data can surprise you with mutable objects such as lists and dictionaries, and that a list should not be a class variable when each object needs its own. Here is the mistake, then the fix:

class BadList:
    items = []
    def add(self, v):
        self.items.append(v)

p = BadList()
q = BadList()
p.add("a")
q.add("b")
print(p.items, q.items)
['a', 'b'] ['a', 'b']

Both objects use the one list on the class. Create the list inside __init__ instead:

class GoodList:
    def __init__(self):
        self.items = []
    def add(self, v):
        self.items.append(v)

p = GoodList()
q = GoodList()
p.add("a")
q.add("b")
print(p.items, q.items)
['a'] ['b']

Now each object has its own list.

6. Inheritance

A new class can build on an existing one. You name the base class in parentheses, and the new class gets its methods; the tutorial adds that it can override any of them. Here a savings account adds an interest rate and one new method:

class Savings(Account):
    def __init__(self, owner, balance=0, rate=0.02):
        Account.__init__(self, owner, balance)
        self.rate = rate

    def add_interest(self):
        self.deposit(self.balance * self.rate)

s = Savings("Ana", 200)
s.add_interest()
print(s.summary())
Ana: 204.0

Account.__init__(self, owner, balance) runs the base class setup, so owner and balance still get stored. The balance is 200 plus 2 percent of 200, which is 4, for 204.0. The result shows a decimal point because multiplying by 0.02 produces a float.

The tutorial names two helper functions for checking relationships:

print(isinstance(s, Account), isinstance(a, Savings), issubclass(Savings, Account))
True False True

A savings account is an account, but the plain account a is not a savings account. The built-in function reference adds that a class counts as a subclass of itself.

7. When a class is worth it

If you only need a few named values, a dictionary may be enough. A class starts to pay off when the same data always travels with the same operations, as balance does with deposit and withdraw, or when you need several independent copies.

8. Key takeaways

  • A class defines a type; calling it creates an instance with its own data.
  • __init__ runs automatically on creation and receives the arguments you pass to the class.
  • self is the instance, passed in implicitly. Leave it out of the parameter list and you get a TypeError.
  • Class variables are shared by every instance. Put lists and dictionaries in __init__ so each object gets its own.
  • class Child(Parent) inherits methods; Parent.__init__(self, ...) runs the parent's setup.
  • isinstance() and issubclass() check what an object or class is built from.

Sources

  1. Python Software Foundation, The Python Tutorial, 9. Classes
  2. Python Software Foundation, Built-in Functions, isinstance() and issubclass()
CategoriesCoding From Zeropythonclassesobjectsbeginners

More on work & tech from other topics