A comprehensive Python example file demonstrating virtually every Python feature, construct, and pattern in a single file.
KitchenSink.py
is an educational resource that showcases Python's extensive capabilities through practical examples. This file serves as a reference guide for Python developers of all levels, containing examples of everything from basic data types to advanced design patterns.
- Data Types: Integers, floats, complex numbers, strings, bytes, None
- Collections: Lists, tuples, sets, dictionaries, defaultdict, Counter, deque
- Control Flow: if/elif/else, for/while loops, match statements, comprehensions
- Functions: Regular functions, lambdas, generators, async functions, decorators
- Classes: Basic OOP, inheritance, abstract classes, dataclasses, metaclasses
- Exception Handling: try/except/finally, custom exceptions, exception chaining
- Type Hints: Complete typing annotations with generics and protocols
- Async/Await: Asynchronous programming with asyncio
- Context Managers: Both using and creating context managers
- Descriptors and Properties: Property decorators and custom descriptors
- Metaclasses: Custom metaclass implementation
- Weak References: Memory-efficient reference handling
- File I/O: Text, binary, JSON, CSV file operations
- Regular Expressions: Pattern matching and text processing
- Threading & Multiprocessing: Concurrent and parallel execution
- Networking: Socket programming and HTTP requests
- Database: SQLite database operations
- Logging: Comprehensive logging configuration
- Testing: Unit testing with unittest and mock
- Singleton: Single instance pattern
- Factory: Object creation pattern
- Observer: Event notification pattern
- Decorator: Behavior extension pattern
- Strategy: Algorithm selection pattern
- Itertools: Advanced iteration tools
- Collections Module: Specialized container datatypes
- String Methods: Comprehensive string manipulation
- Math & Statistics: Mathematical operations and statistical functions
- Date & Time: Datetime handling and timezone support
- Random: Random number generation and selection
- OS & System: Operating system interface
- Command Line Arguments: argparse for CLI applications
python3 KitchenSink.py
The file is organized into clearly marked sections. Each section demonstrates specific Python features with practical examples:
# Navigate to any section, for example:
# Section 6: FUNCTIONS - Line ~120
# Section 7: CLASSES AND OOP - Line ~180
# Section 15: TYPING AND TYPE HINTS - Line ~520
You can import specific classes or functions for testing:
from KitchenSink import BasicClass, generator_function, async_function
# Use the imported items
obj = BasicClass("test")
for value in generator_function():
print(value)
- Python 3.10 or higher (for pattern matching and union types)
- No external dependencies for core functionality
- Optional:
pytest
for running the pytest examples section
This file is perfect for:
- Learning Python: See practical examples of every Python feature
- Interview Preparation: Quick reference for Python concepts
- Code Reviews: Understanding Python best practices and patterns
- Teaching: Comprehensive examples for educational purposes
- Quick Reference: Find syntax and usage examples instantly
The file is organized into 48 major sections:
- Imports and Modules - Standard library imports
- Basic Data Types - Fundamental Python types
- Collections - Data structures
- Operators - All Python operators
- Control Flow - Conditionals and loops
- Functions - Function definitions and types
- Classes and OOP - Object-oriented programming
- Magic Methods - Special methods for classes
- Comprehensions - List, dict, set comprehensions
- Exception Handling - Error handling patterns
- Context Managers - Resource management
- Iterators and Generators - Iteration protocols
- Decorators - Function and class decorators
- Functional Programming - FP concepts in Python
- Typing and Type Hints - Type annotations
- Async/Await - Asynchronous programming
- Metaclasses and Descriptors - Advanced OOP
- Modules and Packages - Import system
- File I/O - File operations
- Regular Expressions - Pattern matching
- Threading and Multiprocessing - Concurrency
- Logging - Logging configuration
- Testing - Unit testing
- Properties and Slots - Class optimizations
- Weak References - Memory management
- Pickle and Serialization - Object serialization
- Introspection - Runtime inspection
- Operator Overloading - Custom operators
- Namespace and Scope - LEGB rule
- Memory Management - Garbage collection
- Performance Optimization - Profiling and caching
- Command Line Arguments - CLI parsing
- Environment Variables - System environment
- Warnings - Warning system
- Zip and Enumerate - Iteration helpers
- Itertools - Advanced iteration
- Collections Module - Specialized containers
- String Methods - String manipulation
- Math and Numbers - Mathematical operations
- Date and Time - Temporal operations
- Random - Randomization
- OS and System - System interface
- Networking - Network operations
- Database - SQLite operations
- Unit Testing with Pytest - Testing framework
- Design Patterns - Common patterns
- Advanced Features - Latest Python features
- Main Execution - Entry point
- Proper use of type hints for better code clarity
- Context managers for resource management
- Proper exception handling patterns
- Clean class design with properties and descriptors
- Efficient use of generators for memory optimization
- Async/await for I/O-bound operations
- Comprehensive docstrings and comments
Feel free to suggest additional Python features or patterns that should be included. This is meant to be a living document that evolves with Python.
This educational resource is provided as-is for learning purposes. Feel free to use, modify, and share.
This file is intentionally comprehensive and includes features that you wouldn't typically use all together in a production application. It's designed as a reference and learning tool, not as an example of how to structure a real Python application.
Created as a comprehensive Python reference - your one-stop shop for Python syntax and features!