19.1. OOP Str
Calling
print(obj)
callsstr(obj)
Calling
str(obj)
callsobj.__str__()
Method
obj.__str__()
must returnstr
Intended for end-users of your class
19.1.1. Default
Object without __str__()
method overloaded prints their memory address:
>>> class User:
... def __init__(self, firstname, lastname):
... self.firstname = firstname
... self.lastname = lastname
>>>
>>>
>>> mark = User('Mark', 'Watney')
>>>
>>> print(mark)
<__main__.User object at 0x...>
>>>
>>> str(mark)
'<__main__.User object at 0x...>'
>>>
>>> mark.__str__()
'<__main__.User object at 0x...>'
19.1.2. Overloaded
Objects can verbose print if __str__()
method is present:
>>> class User:
... def __init__(self, firstname, lastname):
... self.firstname = firstname
... self.lastname = lastname
...
... def __str__(self):
... return f'Hello {self.firstname} {self.lastname}'
>>>
>>>
>>> mark = User('Mark', 'Watney')
>>>
>>> print(mark)
Hello Mark Watney
>>>
>>> str(mark)
'Hello Mark Watney'
>>>
>>> mark.__str__()
'Hello Mark Watney'
19.1.3. Assignments
# %% 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
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`
# %% About
# - Name: Operator String Str
# - Difficulty: easy
# - Lines: 2
# - Minutes: 2
# %% English
# 1. Modify `User` to overwrite `__str__()` method
# 2. Printing object should return firstname and lastname,
# example: Mark Watney
# 3. Run doctests - all must succeed
# %% Polish
# 1. Zmodyfikuj klasę `User` aby nadpisać metodę `__str__()`
# 2. Wypisanie obiektu powinno zwrówić imię i nazwisko,
# przykład: Mark Watney
# 3. Uruchom doctesty - wszystkie muszą się powieść
# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> mark = User('Mark', 'Watney')
>>> print(mark)
Mark Watney
>>> melissa = User('Melissa', 'Lewis')
>>> print(melissa)
Melissa Lewis
"""
# Modify `User` to overwrite `__str__()` method
# Printing object should return firstname and lastname,
# Example: Mark Watney
class User:
def __init__(self, firstname, lastname):
self.firstname = firstname
self.lastname = lastname
# %% 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
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`
# %% About
# - Name: Operator String Str
# - Difficulty: easy
# - Lines: 3
# - Minutes: 3
# %% English
# 1. Modify class `Iris` to overwrite `__str__()` method
# 2. While printing object show: species name and a sum of `self.values`,
# example: `species='setosa', total=9.4`
# 3. Result of sum round to one decimal place
# 4. Run doctests - all must succeed
# %% Polish
# 1. Zmodyfikuj klasę `Iris` aby nadpisać metodę `__str__()`
# 2. Przy wypisywaniu obiektu pokaż: nazwę gatunku i sumę `self.values`,
# przykład: `species='setosa', total=9.4`
# 3. Wynik sumowania zaokrąglij do jednego miejsca po przecinku
# 4. Uruchom doctesty - wszystkie muszą się powieść
# %% Hints
# - `f'{var=:.1f}'`
# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> iris = Iris(
... values=[4.7, 3.2, 1.3, 0.2],
... species='setosa',
... )
>>> print(iris)
species='setosa', total=9.4
>>> iris = Iris(
... values=[7.0, 3.2, 4.7, 1.4],
... species='versicolor',
... )
>>> print(iris)
species='versicolor', total=16.3
"""
# Modify class `Iris` to overwrite `__str__()` method
# While printing object show: species name and a sum of `self.values`,
# Example: `species='setosa', total=9.4`
# Result of sum round to one decimal place
class Iris:
values: list
species: str
def __init__(self, values, species):
self.values = values
self.species = species
def __str__(self):
...