QuickDevGuide

Home › Python

Functions & Classes — Python

Python functions are defined with def. They support default parameters, args (variable positional arguments as a tuple), kwargs (variable keyword arguments as a dict), and type hints (optional but recommended). Lambda functions are anonymous one-liners: lambda x: x 2. They're commonly used with map(), filter(), and sorted(). Decorators (@decorator) wrap functions to extend behavior without modifying them — heavily used in web frameworks. Classes & OOP Python classes use the init method as the constructor, and self as the explicit reference to the instance (unlike this in other languages). Special dunder methods (str, repr, eq, len) customize object behavior. @dataclass reduces boilerplate for data-holding classes.

Key Takeaways