OOP Reference
Class attributes, instance constructors, properties, and slots optimizations.
Initialize instance attributes.
Used In: Orchestrators classes, custom database adapters.
def __init__(self, *args)class App:
def __init__(self, name):
self.name = name
a = App('ETL')
print(a.name)'ETL'Time Complexity: O(1)
Common Mistakes: Returning any object from __init__ (raises TypeError). Only None is allowed.
Related Methods: __new__()
__init__ (initializer) vs __new__ (instance creator).
Custom string representations of class objects.
Used In: Console printing, debugging objects in log streams.
def __str__(self): / def __repr__(self):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))Alex User('Alex')Time Complexity: O(1)
Related Methods: repr()
Define variables shared across all class objects vs variables owned by specific instances.
Used In: Tracking global pools metrics, configurations sharing.
class MyClass:
class_var = val
def __init__(self):
self.inst_var = valclass Database:
connections = 0
def __init__(self, host):
self.host = host
Database.connections += 1
d1 = Database('h1')
d2 = Database('h2')
print(d1.host, Database.connections)h1 2Time Complexity: O(1)
Define decorators binding methods to class level vs helper static namespaces.
Used In: Custom constructor parsing configurations, isolated validation functions.
@classmethod / @staticmethodclass Config:
port = 80
@classmethod
def from_env(cls):
return cls()
@staticmethod
def is_valid(p):
return p > 0
print(Config.is_valid(80))TrueTime Complexity: O(1)
Wrap instance variables with getter and setter methods using property decorator.
Used In: Validating fields range changes in records model objects.
@property / @attribute.setterclass 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)60Time Complexity: O(1)
Call constructor or methods on parent classes.
Used In: Building modular ETL inheritance trees.
super().__init__()class Pipeline:
def __init__(self):
self.active = True
class MyETL(Pipeline):
def __init__(self):
super().__init__()
self.name = 'etl'
etl = MyETL()
print(etl.active)TrueTime Complexity: O(1)
Build class architectures using composition (has-a) instead of inheritance (is-a).
Used In: Structuring clean orchestration architectures.
self.dependency = Dep()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())'Running'Time Complexity: O(1)
Define interface contracts that inheriting subclasses must implement.
Used In: Creating standard adapters configurations interfaces.
from abc import ABC, abstractmethodfrom 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'))'Saving'Time Complexity: O(1)
Related Methods: abc.abstractmethod
Use dataclass decorator to auto-generate boilerplate constructors, __repr__, and comparison methods.
Used In: Configuration objects declarations, table row values schemas.
from dataclasses import dataclass
@dataclassfrom dataclasses import dataclass
@dataclass
class Config:
host: str
port: int
c = Config('localhost', 80)
print(c)Config(host='localhost', port=80)Time Complexity: O(1)
Related Methods: dataclasses.asdict()