16.10. OOP Init Define

  • __init__() - initializer method

  • It's a first method run after object is initiated

  • All classes has default __init__()

constructor

Method called at object instantiation used to create object. Constructor is called on not fully initialized object and hence do not have access to object methods. Constructor should return None.

initializer

Method called at object instantiation used to fill empty object with values. Initializer is called upon object initialization and hence can modify object and use its methods. Initializer should return None.

16.10.1. Without Arguments

Initializer method without arguments:

>>> class User:
...     def __init__(self):
...         print('Hello')
>>>
>>>
>>> mark = User()
Hello

16.10.2. Required Parameters

>>> class User:
...     def __init__(self, firstname, lastname):
...         print(f'Hello {firstname} {lastname}')
>>>
>>>
>>> mark = User()
Traceback (most recent call last):
TypeError: User.__init__() missing 2 required positional arguments: 'firstname' and 'lastname'
>>> class User:
...     def __init__(self, firstname, lastname):
...         print(f'Hello {firstname} {lastname}')
>>>
>>>
>>> mark = User('Mark', 'Watney')
Hello Mark Watney
>>>
>>> mark = User(firstname='Mark', lastname='Watney')
Hello Mark Watney

16.10.3. Optional Parameters

>>> class User:
...     def __init__(self, firstname=None, lastname=None):
...         print(f'Hello {firstname} {lastname}')
>>>
>>>
>>> mark = User('Mark', 'Watney')
Hello Mark Watney
>>>
>>> mark = User('Mark')
Hello Mark None
>>>
>>> mark = User()
Hello None None

16.10.4. Assignments

"""
* Assignment: OOP Init Hello
* Type: class assignment
* Complexity: easy
* Lines of code: 2 lines
* Time: 2 min

English:
    1. Modify `User` class:
       a. Define initialization method
       b. Method should print 'hello' at object creation
    2. Non-functional requirements:
       a. Do not store any values in the class
       b. Run doctests - all must succeed

Polish:
    1. Zmodyfikuj klasę `User`:
       a. Zdefiniuj metodę inicjalizacyjną
       b. Metoda powinna wypisać 'hello' przy tworzeniu obiektu
    2. Wymagania niefunkcjonalne:
       a. Nie przechowuj żadnych wartości w klasie
       b. Uruchom doctesty - wszystkie muszą się powieść

Tests:
    >>> import sys; sys.tracebacklimit = 0
    >>> from inspect import isclass

    >>> assert isclass(User)
    >>> assert hasattr(User, '__init__')

    >>> result = User()
    hello
    >>> vars(result)
    {}
"""

# Define initialization method
# Method should print 'hello' at object creation
class User:
    ...

"""
* Assignment: OOP Init Fields
* Type: class assignment
* Complexity: easy
* Lines of code: 3 lines
* Time: 2 min

English:
    1. Modify `User` class:
       a. Define initialization method
       b. Method should set `firstname` attribute to 'Mark'
       c. Method should set `lastname` attribute to 'Watney'
    2. Non-functional requirements:
       a. Run doctests - all must succeed

Polish:
    1. Zmodyfikuj klasę `User`:
       a. Zdefiniuj metodę inicjalizacyjną
       b. Metoda powinna ustawić atrybut `firstname` na 'Mark'
       c. Metoda powinna ustawić atrybut `lastname` na 'Watney'
    2. Wymagania niefunkcjonalne:
       a. Uruchom doctesty - wszystkie muszą się powieść

Tests:
    >>> import sys; sys.tracebacklimit = 0
    >>> from inspect import isclass

    >>> assert isclass(User)
    >>> assert hasattr(User, '__init__')

    >>> result = User()
    >>> vars(result)
    {'firstname': 'Mark', 'lastname': 'Watney'}
"""

# Define initialization method
# Method should set `firstname` attribute to 'Mark'
# Method should set `lastname` attribute to 'Watney'
class User:
    ...

"""
* Assignment: OOP Init Parameters
* Type: class assignment
* Complexity: easy
* Lines of code: 3 lines
* Time: 2 min

English:
    1. Modify `User` class:
       a. Define initialization method with parameters `firstname` and `lastname`
       b. Method should set `firstname` attribute to the value of `firstname` parameter
       c. Method should set `lastname` attribute to the value of `lastname` parameter
    2. Non-functional requirements:
       a. Run doctests - all must succeed

Polish:
    1. Zmodyfikuj klasę `User`:
       a. Zdefiniuj metodę inicjalizacyjną z parametrami `firstname` i `lastname`
       b. Metoda powinna ustawić atrybut `firstname` na wartość parametru `firstname`
       c. Metoda powinna ustawić atrybut `lastname` na wartość parametru `lastname`
    2. Wymagania niefunkcjonalne:
       a. Uruchom doctesty - wszystkie muszą się powieść

Tests:
    >>> import sys; sys.tracebacklimit = 0
    >>> from inspect import isclass

    >>> assert isclass(User)
    >>> assert hasattr(User, '__init__')

    >>> mark = User('Mark', 'Watney')
    >>> vars(mark)
    {'firstname': 'Mark', 'lastname': 'Watney'}

    >>> mark = User(firstname='Melissa', lastname='Lewis')
    >>> vars(mark)
    {'firstname': 'Melissa', 'lastname': 'Lewis'}
"""

# Define initialization method with parameters `firstname` and `lastname`
# Method should set `firstname` attribute to the value of `firstname` parameter
# Method should set `lastname` attribute to the value of `lastname` parameter
class User:
    ...