16.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.

16.1.1. Classes

  • Classes are templates for objects

Classes should have capitalized name:

>>> class User:
...     pass

16.1.2. Class Names

  • PascalCase name convention

Multi-word class names should use PascalCase:

>>> class SuperUser:
...     pass

16.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 User:
...     pass

16.1.4. Use Case - 0x01

>>> class Guest:
...     pass
>>>
>>>
>>> class User:
...     pass
>>>
>>>
>>> class Admin:
...     pass

16.1.5. Assignments

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

English:
    1. Define class `Account`
    2. Run doctests - all must succeed

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

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

    >>> assert isclass(Account)
"""

# Define class `Account`
# type: type
...


"""
* Assignment: OOP Class Many
* Type: class assignment
* Complexity: easy
* Lines of code: 4 lines
* Time: 2 min

English:
    1. Define class `User`
    2. Define class `Admin`
    3. Run doctests - all must succeed

Polish:
    1. Zdefiniuj klasę `User`
    2. Zdefiniuj klasę `Admin`
    3. Uruchom doctesty - wszystkie muszą się powieść

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

    >>> assert isclass(User)
    >>> assert isclass(Admin)
"""

# Define class `User`
# type: type
...

# Define class `Admin`
# type: type
...