12.1. OOP Class Define

  • Object Oriented Paradigm

  • Model world as objects that interacts with each other

  • Classes are templates for objects

  • PascalCase name convention

  • Never print in a class

class

Templates for objects.

12.1.1. Classes

  • Classes are templates for objects

Classes should have capitalized name:

>>> class Astronaut:
...     pass

12.1.2. Class Names

  • PascalCase name convention

Multi-word class names should use PascalCase:

>>> class SeniorAstronaut:
...     pass

12.1.3. Good Practices

  • Never print in a class

  • All classes in one file - when classes are short

  • One class per file - when classes are long

You can mix classes and functions in one file:

>>> def say_hello():
...     pass
>>>
>>>
>>> class Astronaut:
...     pass

12.1.4. Use Case - 0x01

>>> class AstronautEngineer:
...     pass
>>>
>>>
>>> class AstronautScientist:
...     pass
>>>
>>>
>>> class AstronautPilot:
...     pass

12.1.5. Assignments

Code 12.1. Solution
"""
* Assignment: OOP Class Define A
* Required: yes
* Complexity: easy
* Lines of code: 2 lines
* Time: 2 min

English:
    1. Define class `Iris`
    5. Run doctests - all must succeed

Polish:
    1. Zdefiniuj klasę `Iris`
    5. Uruchom doctesty - wszystkie muszą się powieść

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

    >>> assert isclass(Iris)
"""


Code 12.2. Solution
"""
* Assignment: OOP Class Define B
* Required: yes
* Complexity: easy
* Lines of code: 4 lines
* Time: 2 min

English:
    1. Define class `Astronaut`
    2. Define class `SpaceAgency`
    5. Run doctests - all must succeed

Polish:
    1. Zdefiniuj klasę `Astronaut`
    2. Zdefiniuj klasę `SpaceAgency`
    5. Uruchom doctesty - wszystkie muszą się powieść

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

    >>> assert isclass(Astronaut)
    >>> assert isclass(SpaceAgency)
"""