6.10. Optimization Exception

6.10.1. Check for Permission

>>> class User:
...    def __init__(self, name):
...        self.name = name
>>>
>>>
>>> alice = User('Alice')
>>>
>>> if hasattr(alice, 'name'):
...     print(alice.name)
... else:
...     print('Invalid attribute')
Alice
>>>
>>> if hasattr(alice, 'age'):
...     print(alice.age)
... else:
...     print('Invalid attribute')
Invalid attribute

6.10.2. Ask for Forgiveness

>>> class User:
...    def __init__(self, name):
...        self.name = name
>>>
>>>
>>> alice = User('Alice')
>>>
>>> try:
...     print(alice.name)
... except AttributeError:
...     print('No name')
Alice
>>>
>>> try:
...     print(alice.age)
... except AttributeError:
...     print('Invalid attribute')
Invalid attribute

6.10.3. Multiple Attributes

>>> class User:
...    def __init__(self, firstname, lastname, email):
...        self.firstname = firstname
...        self.lastname = lastname
...        self.email = email
>>>
>>>
>>> alice = User('Alice', 'Apricot', email='alice@example.com')
>>>
>>> try:
...     print(alice.firstname)
...     print(alice.lastname)
...     print(alice.email)
...     print(alice.age)
... except AttributeError:
...     print('Invalid attribute')
Alice
Apricot
alice@example.com
Invalid attribute

6.10.4. Assignments

# %% About
# - Name: Unpack Parameters Define
# - Difficulty: easy
# - Lines: 4
# - Minutes: 3

# %% 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. Create function `mean()`, which calculates arithmetic mean
# 2. Function takes arbitrary number of `int` or `float` arguments
# 3. If no arguments are given, the function should raise `ValueError` with message "Invalid argument"
# 4. Non-functional requirements:
#    - User will pass only `int` or `float` nothing else
#    - Do not import any libraries and modules
#    - Use builtin functions `sum()` and `len()`
# 5. Run doctests - all must succeed

# %% Polish
# 1. Napisz funkcję `mean()`, wyliczającą średnią arytmetyczną
# 2. Funkcja przyjmuje dowolną liczbę argumentów `int` lub `float`
# 3. Jeżeli nie podano argumentów, to funkcja powinna podnieść `ValueError` z komunikatem "Invalid argument"
# 4. Wymagania niefunkcjonalne:
#    - Użytkownik poda tylko `int` lub `float` nic innego
#    - Nie importuj żadnych bibliotek i modułów
#    - Użyj wbudowanych funkcji `sum()` i `len()`
# 5. Uruchom doctesty - wszystkie muszą się powieść

# %% Hints
# - `raise ValueError('error message')`
# - `sum(...) / len(...)`
# - https://docs.python.org/3/whatsnew/3.11.html#misc

# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0

>>> assert sys.version_info >= (3, 9), \
'Python has an is invalid version; expected: `3.9` or newer.'

>>> mean(1)
1.0
>>> mean(1, 2)
1.5
>>> mean(1, 2, 3)
2.0
>>> mean(1, 2, 3, 4)
2.5
>>> mean()
Traceback (most recent call last):
ValueError: Invalid argument
"""

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

# %% Imports

# %% Types
from typing import Callable
mean: Callable[[tuple[int|float]], float | int]

# %% Data

# %% Result