Python
Dunder (Magic) Methods
| Method | Description |
|---|---|
| __new__ | Creates a new instance of a class. |
| __init__ | Initializes an object when it is created. |
| __call__ | Enables objects to be called like functions. |
| __dir__ | Returns the attributes of a class. |
| __repr__ | Returns an unambiguous string representation of the object. |
| __str__ | Returns a readable string representation of the object. |
| __len__ | Returns the length of the object. |
| __getitem__ | Enables indexing and slicing operations. |
| __setitem__ | Enables setting values using indexing. |
| __delitem__ | Enables deletion of items using indexing. |
| __iter__ | Returns an iterator object for iteration. |
| __next__ | Retrieves the next item in the iteration sequence. |
| __enter__ | Sets up the context for a with statement. |
| __exit__ | Cleans up resources when the with statement exits. |
| __getattr__ | Handles attribute access when an attribute is not found. |
| __setattr__ | Handles setting attribute values. |
| __delattr__ | Handles deletion of attributes. |
| __eq__ | Defines equality comparison. |
| __ne__ | Defines inequality comparison. |
| __lt__ | Defines less than comparison. |
| __gt__ | Defines greater than comparison. |
| __le__ | Defines less than or equal to comparison. |
| __ge__ | Defines greater than or equal to comparison. |
| __del__ | Cleans up resources when an object is about to be destroyed. |
| __hash__ | Returns the hash value of the object. |
PEP 20 - The Zen of Python
- 1. Beautiful is better than ugly.
- 2. Explicit is better than implicit.
- 3. Simple is better than complex.
- 4. Complex is better than complicated.
- 5. Flat is better than nested.
- 6. Sparse is better than dense.
- 7. Readability counts.
- 8. Special cases aren't special enough to break the rules.
- 9. Although practicality beats purity.
- 10. Errors should never pass silently.
- 11. Unless explicitly silenced.
- 12. In the face of ambiguity, refuse the temptation to guess.
- 13. There should be one-- and preferably only one --obvious way to do it.
- 14. Although that way may not be obvious at first unless you're Dutch.
- 15. Now is better than never.
- 16. Although never is often better than *right* now.
- 17. If the implementation is hard to explain, it's a bad idea.
- 18. If the implementation is easy to explain, it may be a good idea.
- 19. Namespaces are one honking great idea -- let's do more of those!
Useful Notes
PEP 8 (Style Guide)
black | isort | pylint | mypy | ruff | ty
PEP 484
Type Hints
Immutables
tuple | frozenset | int | float | str | bool
Testing
pytest
Dependency Manager
pip | pdm | uv
Virtual Environments
venv