Legends ofPythos
Claim your name
Objects and Classes

Multiple inheritance and private names

Lesson 10 of 13

Watch the lesson5:54 · with Torsten
So far you have seen a class inherit from one parent. Python lets it do more: class FlyingCar(Car, Plane) makes FlyingCar pull methods from both parents.

Method resolution order

When two parents have the same method name, Python picks one by following a fixed rule called the MRO. It checks the class itself first, then walks through the parent list from left to right. You can see this exact order with ClassName.__mro__ or ClassName.mro(). Each class's __name__ is its name as text, so the list comprehension in the example below turns the classes into names. Last in the order comes object, a class built into Python: every class has it as a parent, even if you don't write it.
class Car:
    def move(self):
        return 'drives'

class Plane:
    def move(self):
        return 'flies'

class FlyingCar(Car, Plane):
    pass

# drives: Car is listed first
print(FlyingCar().move())

# The search order, as class names
classes = FlyingCar.__mro__
order = [c.__name__ for c in classes]
print(order)
# ['FlyingCar', 'Car', 'Plane', 'object']
FlyingCar, then Car, then Plane: left to right through the parents

Mixins

Small helper classes that each add one behaviour are called mixins. Below, JsonMixin has just one method, to_json(), and Truck gets it by listing the mixin first, before its main parent, Vehicle. Inside it, self.__dict__ is a dictionary of every attribute stored on the object, and json.dumps(d, sort_keys=True) turns that into JSON text, with the keys in A to Z order. Truck has no __init__ of its own, so Python searches the MRO and uses Vehicle's.
import json

class JsonMixin:
    def to_json(self):
        d = self.__dict__
        return json.dumps(d, sort_keys=True)

class Vehicle:
    def __init__(self, name):
        self.name = name

# One mixin, then the main parent
class Truck(JsonMixin, Vehicle):
    pass

t = Truck('Hauler')
print(t.to_json())   # {"name": "Hauler"}
A mixin adds one method, and no data of its own

Name mangling: double underscore names

class BankAccount:
    def __init__(self, balance):
        self.__balance = balance

class Sneaky(BankAccount):
    def __init__(self, balance):
        super().__init__(balance)
        # Becomes _Sneaky__balance: no clash
        self.__balance = 0

s = Sneaky(100)
# The parent's value is untouched:
print(s._BankAccount__balance)  # 100
print(s._Sneaky__balance)       # 0
# print(s.__balance) gives an AttributeError
self.__x inside a class becomes _ClassName__x
Writing self.__balance tells Python to store it as _BankAccount__balance. A subclass that accidentally writes the same name gets its own mangled slot, so nothing is silently overwritten. It is a guard against collisions — not real privacy, because anyone can still reach the mangled name. For everyday code you'll usually use a single underscore instead (_balance), a polite "please don't touch" sign that the next lesson covers.

Your turn

0 of 3 solved

Exercise 1

+40 XP
Write a class Car with a method move() returning 'drives', and a class Plane whose move() returns 'flies'. Then write class FlyingCar(Car, Plane) and class CarPlane(Plane, Car), each with just pass as its body. Finally store [c.__name__ for c in FlyingCar.__mro__] in order. FlyingCar.__mro__ holds the classes Python searches, in that order, and each class's __name__ is its name as text.
class Car:
    pass




class Plane:
    pass




# FlyingCar(Car, Plane) and CarPlane(Plane, Car)


order = []

Run your code to check it against the tests.

Exercise 2

+40 XP
A mixin adds one behaviour to any class. Write JsonMixin with a method to_json() that returns json.dumps(self.__dict__, sort_keys=True): self.__dict__ is a dictionary of every attribute stored on the object, and sort_keys=True puts its keys in A to Z order. Write a plain class Item whose __init__(self, name, price) stores both. Then combine them: class Product(JsonMixin, Item) with just pass, and class Customer(JsonMixin) whose __init__(self, name, email) stores both. For example, Product('axe', 12).to_json() is '{"name": "axe", "price": 12}'.
import json




class JsonMixin:
    pass




class Item:
    pass




# Product and Customer go here

Run your code to check it against the tests.

Exercise 3

+40 XP
Write a class Account that keeps its money in self.__balance (two underscores), with deposit(amount) adding to it and balance() returning it. Then write Savings(Account), whose __init__ calls super().__init__(balance) and then sets its own self.__balance = 0 for its own records. Name mangling stores the two as _Account__balance and _Savings__balance, so the subclass cannot clobber the account's money.
class Account:
    def __init__(self, balance):
        pass


    # deposit and balance go here




class Savings(Account):
    def __init__(self, balance):
        pass

Run your code to check it against the tests.