TL;DR You need to store arbitrary objects, potentially with mixed data types? $\rightarrow$ Use list (mutable) or tuple (immutable) You have numeric (integer or floating point) data and tight packing and performance is important?
2023-01-04
TL;DR Dictionaries are the central data structure in Python THe built-in dict type is “good enough” most of the time Specialized implementations, like read-only or ordered dicts, are also available in the Python standard library.
2022-12-19
TL:DR Instance methods need a class instance and can access the instance through self. Class methods don’t need a class instance. They can’t accessthe instance (self) but they have access to the class itself via cls.
2022-12-16
TL;DR Class variables are for data shared by all instances of a class. They belong to a class, not a specific instance and are shared among all instances of a class.
2022-12-16
TL;DR Abstract Base Classes (ABCs) ensure that derived classes implement particular methods from the base class at instantiation time Using Python’sabc module help avoid bugs and make class hierarchies easier to maintain.
2022-12-11
TL;DR Making a shallow copy of an object won’t clone child objects. → The copy is not fully independent of the original. A deep copy of an object will recursively clone child objects.
2022-12-11
TL;DR Defining your own exception types will state your code’s intent more clearly and make it easier to debug. Derive your custom exceptions from Python’s built-in Exception class or from more specific exception classes like ValueError or KeyError.
2022-12-04
TL;DR Control to-string conversion in your own classes using the __str__ and __repr__ “dunder” methods The result of __str__ should be readable. The result of __repr__ should be unambiguous. Always add a __repr__ to your classes.
2022-12-04
The == operator compares by checking for equality (the objects referred to by the variables are equal, i.e. have the same contents) The is operator comparies identities (whether two variables are in fact pointing to the same identical object) Example
2022-12-04
Python adds an implicit return None statement to the end of any function. In other words, if a function doesn’t specify a return value, it returns None by default. This means you can replace return None statements with bare return statements or even leave them out completely and still get the same result.
2022-12-04