__init__
The constructor method called automatically when a new instance of a class is created. Receives the arguments passed to the class and uses them to set up the instance's fields. Without it you cannot pass any values when creating an instance. !r
Adds repr() formatting to a value inside an f-string, wrapping strings in quotes and showing escape characters explicitly. Useful for debugging and error messages where you want to see the exact value including its type hints. Without !r a string is inserted as plain text; with !r it is wrapped in quotes so it's unambiguous. |
__repr__
Defines how an object is represented as a string when printed or inspected. Should return a string that clearly describes the object and its contents. Used by print(), debuggers, and the interactive console. |
__eq__
Defines what equality means for two instances of a class. Without it, == checks if two variables point to the same object in memory rather than comparing their contents. Should return True if all relevant fields are equal, False otherwise. |
Cheatography
https://cheatography.com
Python Cheat Sheet (DRAFT) by blakecromar
Generalized Python Code
This is a draft cheat sheet. It is a work in progress and is not finished yet.