ruk·si

🐍 Python
Basics

Updated at 2018-06-09 12:44

This note contains basic Python syntax overview. The example contains optional (but recommended) type hinting syntax.

from typing import Dict, Tuple

class MyClass:
    """This is MyClass documentation."""
    one: int
    two: int
    three: int = 3

    def __init__(self, one: int, two: int = 2) -> None:
        self.one = one
        self.two = two
        print('MyClass!')

    def get_three(self) -> int:
        return self.three

    def get_arguments(self, *args, **kwargs) -> Tuple[Tuple, Dict]:
        return args, kwargs

class SubClass(MyClass):
    four: int = 4

    def __init__(self, one: int, two: int = 2) -> None:
        super().__init__(one, two)
        print('SubClass!')

    def get_four(self) -> int:
        """This is method documentation."""
        return self.four

x = SubClass(1)  # => MyClass!, SubClass!
assert (x.one, x.two, x.three) == (1, 2, 3)
assert x.get_three() == 3
assert x.get_four() == 4
assert x.get_arguments('a', 'b') == (('a', 'b'), {})
assert x.get_arguments(a='aa', b='bb') == ((), {'a': 'aa', 'b': 'bb'})
assert x.get_arguments('a', b='bb') == (('a',), {'b': 'bb'})