16.10. OOP Override

  • Child inherits all fields and methods from parent

  • Used to avoid code duplication

  • When child has method or attribute with the same name as parent

  • In such case child attribute will be used (will overload parent)

overload

When child has method or attribute with the same name as parent. In such case child attribute will be used (will overload parent).

16.10.1. Overload Method

>>> class User:
...     def login(self):
...         print('User login')
>>>
>>>
>>> class Admin(User):
...     def login(self):
...         print('Admin login')
>>>
>>>
>>> mark = Admin()
>>> mark.login()
Admin login

16.10.2. Overload Attribute

>>> class User:
...     def __init__(self):
...         self.firstname = 'Mark'
...         self.lastname = 'Watney'
...         self.is_admin = False
>>>
>>> class Admin(User):
...     def __init__(self):
...         self.is_admin = True
>>>
>>>
>>> mark = Admin()
>>> vars(mark)
{'is_admin': True}

16.10.3. Super

  • super() Calls a method from superclass

  • Order/location is important

  • Raymond Hettinger - Super considered super! - PyCon 2015 [1]

>>> class User:
...     def __init__(self):
...         self.firstname = 'Mark'
...         self.lastname = 'Watney'
...         self.is_admin = False
>>>
>>> class Admin(User):
...     def __init__(self):
...         super().__init__()
...         self.is_admin = True
>>>
>>> mark = Admin()
>>> vars(mark)
{'firstname': 'Mark', 'lastname': 'Watney', 'is_admin': True}

16.10.4. Super Init with Args

>>> class User:
...     def __init__(self, firstname, lastname):
...         self.firstname = firstname
...         self.lastname = lastname
...         self.is_admin = False
>>>
>>>
>>> class Admin(User):
...     def __init__(self, firstname, lastname):
...         super().__init__(firstname, lastname)
...         self.is_admin = True
>>>
>>>
>>> mark = Admin('Mark', 'Watney')
>>> vars(mark)
{'firstname': 'Mark', 'lastname': 'Watney', 'is_admin': True}

16.10.5. References

16.10.6. Assignments

# %% About
# - Name: OOP Override Method
# - Difficulty: easy
# - Lines: 2
# - Minutes: 2

# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author

# %% English
# 1. Use class `User`
# 2. Modify class `Admin` to display `Admin login` in `login()` method
# 3. Do not change inheritance
# 4. Run doctests - all must succeed

# %% Polish
# 1. Użyj klasy `User`
# 2. Zmodyfikuj klasę `Admin` tak aby w metodzie `login()` wyświetlała `Admin login`
# 3. Nie zmieniaj dziedziczenia
# 4. Uruchom doctesty - wszystkie muszą się powieść

# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'

>>> mark = User()
>>> mark.login()
User login

>>> melissa = Admin()
>>> melissa.login()
Admin login
"""

# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`

# %% Imports

# %% Types
from typing import Callable
User: type
Admin: type
login: Callable[[object], None]

# %% Data
class User:
    def login(self):
        print('User login')

# %% Result
class Admin(User):
    ...

# %% About
# - Name: OOP Override Super
# - Difficulty: easy
# - Lines: 1
# - Minutes: 2

# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author

# %% English
# 1. Use class `User`
# 2. Use class `Admin` inheriting from `User`
# 3. In `Admin.login()` method call `login()` method of a parent class
# 4. Use `super()` method
# 5. Do not change inheritance
# 6. Run doctests - all must succeed

# %% Polish
# 1. Użyj klasy `User`
# 2. Użyj klasy `Admin` dziedziczącej po `User`
# 3. W metodzie `Admin.login()` wywołaj metodę `login()` klasy nadrzędnej
# 4. Użyj metody `super()`
# 5. Uruchom doctesty - wszystkie muszą się powieść

# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'

>>> mark = User('Mark', 'Watney')
>>> mark.login()
User login

>>> melissa = User('Melissa', 'Lewis')
>>> melissa.login()
User login
"""

# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`

# %% Imports

# %% Types
from typing import Callable
User: type
Admin: type
__init__: Callable[[object], None]
login: Callable[[object], None]

# %% Data
class User:
    def __init__(self, firstname, lastname):
        self.firstname = firstname
        self.lastname = lastname

    def login(self):
        print('User login')

# %% Result
class Admin(User):
    def login(self):
        ...