PYTHON Reference Guide
Revision Time: 3 mins

OOP Reference

Class attributes, instance constructors, properties, and slots optimizations.

__init__
Return: None

Initialize instance attributes.

Used In: Orchestrators classes, custom database adapters.

Syntax signature:def __init__(self, *args)
Code snippet:
python
class App:
    def __init__(self, name):
        self.name = name
a = App('ETL')
print(a.name)
Expected Output:'ETL'

Time Complexity: O(1)

Common Mistakes: Returning any object from __init__ (raises TypeError). Only None is allowed.

Related Methods: __new__()

Comparison:

__init__ (initializer) vs __new__ (instance creator).

Remember: self refers to the current instance object.
__str__ vs __repr__
Return: str

Custom string representations of class objects.

Used In: Console printing, debugging objects in log streams.

Syntax signature:def __str__(self): / def __repr__(self):
Code snippet:
python
class User:
    def __init__(self, name):
        self.name = name
    def __str__(self):
        return self.name
    def __repr__(self):
        return f"User('{self.name}')"
u = User('Alex')
print(str(u), repr(u))
Expected Output:Alex User('Alex')

Time Complexity: O(1)

Related Methods: repr()

Remember: __str__ is user-friendly formatting; __repr__ is unambiguous developer diagnostic format (often evaluable python expression).
Instance Variables vs Class Variables
Return: None

Define variables shared across all class objects vs variables owned by specific instances.

Used In: Tracking global pools metrics, configurations sharing.

Syntax signature:class MyClass: class_var = val def __init__(self): self.inst_var = val
Code snippet:
python
class Database:
    connections = 0
    def __init__(self, host):
        self.host = host
        Database.connections += 1
d1 = Database('h1')
d2 = Database('h2')
print(d1.host, Database.connections)
Expected Output:h1 2

Time Complexity: O(1)

Remember: Modifying a class variable updates it for all instances, whereas instance variables are private to each object instance.
@classmethod vs @staticmethod
Return: None

Define decorators binding methods to class level vs helper static namespaces.

Used In: Custom constructor parsing configurations, isolated validation functions.

Syntax signature:@classmethod / @staticmethod
Code snippet:
python
class Config:
    port = 80
    @classmethod
    def from_env(cls):
        return cls()
    @staticmethod
    def is_valid(p):
        return p > 0
print(Config.is_valid(80))
Expected Output:True

Time Complexity: O(1)

Remember: classmethod receives the class object (cls) as first argument (useful for custom factory constructors); staticmethod receives no implicitly bound variables.
@property (Encapsulation)
Return: None

Wrap instance variables with getter and setter methods using property decorator.

Used In: Validating fields range changes in records model objects.

Syntax signature:@property / @attribute.setter
Code snippet:
python
class Salary:
    def __init__(self, val):
        self._val = val
    @property
    def val(self):
        return self._val
    @val.setter
    def val(self, new_val):
        if new_val > 0: self._val = new_val
s = Salary(50)
s.val = 60
print(s.val)
Expected Output:60

Time Complexity: O(1)

Remember: Encapsulates values settings behind clean property getters and checks setter bounds safely.
Inheritance & super()
Return: None

Call constructor or methods on parent classes.

Used In: Building modular ETL inheritance trees.

Syntax signature:super().__init__()
Code snippet:
python
class Pipeline:
    def __init__(self):
        self.active = True
class MyETL(Pipeline):
    def __init__(self):
        super().__init__()
        self.name = 'etl'
etl = MyETL()
print(etl.active)
Expected Output:True

Time Complexity: O(1)

Remember: super() delegates constructor calls following the Method Resolution Order (MRO) hierarchy.
Composition vs Inheritance
Return: None

Build class architectures using composition (has-a) instead of inheritance (is-a).

Used In: Structuring clean orchestration architectures.

Syntax signature:self.dependency = Dep()
Code snippet:
python
class Engine:
    def run(self): return 'Running'
class Pipeline:
    def __init__(self):
        self.engine = Engine()
    def start(self):
        return self.engine.run()
print(Pipeline().start())
Expected Output:'Running'

Time Complexity: O(1)

Remember: Composition keeps classes loosely coupled. Prefer composition over inheritance when designs are complex.
Abstract Base Classes (abc.ABC)
Return: None

Define interface contracts that inheriting subclasses must implement.

Used In: Creating standard adapters configurations interfaces.

Syntax signature:from abc import ABC, abstractmethod
Code snippet:
python
from abc import ABC, abstractmethod
class Writer(ABC):
    @abstractmethod
    def write(self, data):
        pass
class ParquetWriter(Writer):
    def write(self, data): return 'Saving'
print(ParquetWriter().write('data'))
Expected Output:'Saving'

Time Complexity: O(1)

Related Methods: abc.abstractmethod

Remember: Instantiation of abstract base classes directly is blocked; children must implement all @abstractmethod declarations.
Dataclasses (@dataclass)
Return: None

Use dataclass decorator to auto-generate boilerplate constructors, __repr__, and comparison methods.

Used In: Configuration objects declarations, table row values schemas.

Syntax signature:from dataclasses import dataclass @dataclass
Code snippet:
python
from dataclasses import dataclass
@dataclass
class Config:
    host: str
    port: int
c = Config('localhost', 80)
print(c)
Expected Output:Config(host='localhost', port=80)

Time Complexity: O(1)

Related Methods: dataclasses.asdict()

Remember: Saves code space from manual constructors boilerplate. Can be made immutable using @dataclass(frozen=True).