# Advanced Class Mechanics

> How does super() work in Python with multiple inheritance? What are mixins?

Canonical: https://www.internhack.in/learn/interview/python-interview/python-class-advanced
Last updated: 2026-11-28

Difficulty: Advanced

## Question

How does super() work in Python with multiple inheritance? What are mixins?

## Answer

Python uses Method Resolution Order (MRO) - a linear ordering of base classes computed by the C3 linearization algorithm.

super() follows the MRO - it calls the next class in the MRO, not necessarily the direct parent. This is crucial for cooperative multiple inheritance.

Mixins: small, focused classes that add behavior to other classes without being used standalone. They should not define \__init_\_ (or call super().\__init_\_() cooperatively).

\__init_subclass_\_: called whenever a class is subclassed, enabling plugin registries and validation.

## Code examples

### MRO and Cooperative super()

```
class A:
    def method(self):
        print('A.method')
        super().method()  # Cooperative - passes to next in MRO

class B(A):
    def method(self):
        print('B.method')
        super().method()

class C(A):
    def method(self):
        print('C.method')
        super().method()

class D(B, C):
    def method(self):
        print('D.method')
        super().method()

print(D.__mro__)  # D -> B -> C -> A -> object
d = D()
d.method()
# D.method -> B.method -> C.method -> A.method

# super() follows MRO, not just the parent
# B.super() goes to C (next in D's MRO), not A!

# Verify MRO
for cls in D.__mro__:
    print(cls.__name__)
```

super() in B calls C, not A - it follows the MRO of the instance (D), not B's own parent. This is cooperative multiple inheritance.

### Mixins and \__init_subclass_\_

```
# Mixin: no __init__, no standalone use
class LoggedMixin:
    def __getattribute__(self, name):
        result = super().__getattribute__(name)
        if callable(result):
            print(f'Accessing: {name}')
        return result

class JSONMixin:
    def to_json(self):
        import json
        return json.dumps(vars(self))

    @classmethod
    def from_json(cls, data):
        import json
        return cls(json.loads(data))

# Compose with multiple inheritance
class User(JSONMixin, LoggedMixin):
    def __init__(self, name, email):
        self.name = name
        self.email = email

u = User('Alice', 'alice@example.com')
print(u.to_json())

# __init_subclass__: plugin registry
class BasePlugin:
    _plugins = {}

    def __init_subclass__(cls, plugin_name=None, kwargs):
        super().__init_subclass__(kwargs)
        if plugin_name:
            BasePlugin._plugins[plugin_name] = cls

class EmailPlugin(BasePlugin, plugin_name='email'):
    def send(self): print('Sending email')

class SMSPlugin(BasePlugin, plugin_name='sms'):
    def send(self): print('Sending SMS')

print(BasePlugin._plugins)
# {'email': EmailPlugin, 'sms': SMSPlugin}
```

\__init_subclass_\_ runs when any subclass is defined - enables automatic plugin registration without explicit registration calls.

## Key points

- C3 linearization ensures each class appears once in MRO and respects inheritance order
- Cooperative multiple inheritance requires ALL classes to use super() correctly
- Mixins should be stateless or have no conflicting attribute names
- \__init_subclass_\_ is called with keyword args from the class definition

## Common follow-up questions

- How does Python compute the MRO (C3 linearization)?
- What happens if two base classes define the same attribute?
- When would you use \__init_subclass_\_ over class decorators or metaclasses?

## Interview tips

- MRO and cooperative super() are advanced topics - great for senior roles
- Show the D -> B -> C -> A order clearly - the surprise that B.super() goes to C
- Mixins with no \__init_\_ are the Pythonic composition pattern - contrast with Java interfaces

## Concepts covered

\__init_subclass_\_, ABC, mixin, multiple inheritance, super()

Previous: [Python Data Model and \__dunder_\_ Methods](https://www.internhack.in/learn/interview/python-interview/python-datamodel) · [All of Python Interview](https://www.internhack.in/learn/interview/python-interview)

## More Python Interview questions

- [Generators & Iterators](https://www.internhack.in/learn/interview/python-interview/python-generators)
- [GIL & Concurrency](https://www.internhack.in/learn/interview/python-interview/python-gil)
- [Variables & Data Types](https://www.internhack.in/learn/interview/python-interview/python-variables-types)
- [Strings & String Methods](https://www.internhack.in/learn/interview/python-interview/python-strings)
- [Lists, Tuples & Sets](https://www.internhack.in/learn/interview/python-interview/python-lists-tuples-sets)
- [Dictionaries & Operations](https://www.internhack.in/learn/interview/python-interview/python-dictionaries)

## Related

- [Learn](https://www.internhack.in/learn)
- [Interview Prep](https://www.internhack.in/learn/interview)
- [Python Interview](https://www.internhack.in/learn/interview/python-interview)
