You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
f strings (including self-documenting with f'{var=}')
import this (zen of Python)
no need to inherit from object (already done implicitly)
functools.wraps for keeping docstring and metadata for decorated functions
__class__.__name__ in __repr__ definitions (and __str__ if the name makes sense to display)
define at least a __repr__, because __str__ falls back on it if undefined
__repr__ should be unambiguous, __str__ should be readable
good practice to define a base exception for distributed packages
copy module copy and deepcopy functions work on arbitrary objects (including custom classes)
comprehensions are faster and generally more efficient than manual creation
abs.ABC doesn't allow instantiation of the base class, and raises an error if methods aren't overridden instead of at call time (specifically helpful for instantiable classes, although likely most abstract classes are intended to be instantiable)
typing.NamedTuple is a memory-efficient shortcut to making immutable classes with a free __repr__ method, named access, and iterability, and reasonably nice definition syntax
types.SimpleNamespace is a dictionary with dot-accessible keys
instance properties 'shadow' class properties with the same name (the instance gets asked first, and used unless explicitly called as obj.class.prop)
factory functions are used to create standard/common instances with different parameters to those normally required (e.g. a VideoWriter from a stream (internal/measurable framerate) instead of from user-generated images)
make sure @Property is mentioned - setters and getters give actual control over what's accessible to the public interface (although internals are still technically findable/accessible)
dictionary updates with .update, and creation with defaults {'default': 3, **override}
tkinter -> tkinter.ttk (more modern and usable widgets)
underscores in numbers are ignored (can be used for visual separation)
3.8 has positional-only and keyword-only arguments (preceded by special arguments '/' and '*') -> raise TypeError if caller specifies a positional only argument as a keyword
methods defined in a class scope can use class-variables as default values, but can only be used within the method definition if a reference is provided to the class/instance
The text was updated successfully, but these errors were encountered:
f'{var=}'
)import this
(zen of Python)object
(already done implicitly)functools.wraps
for keeping docstring and metadata for decorated functions__class__.__name__
in__repr__
definitions (and__str__
if the name makes sense to display)__repr__
, because__str__
falls back on it if undefined__repr__
should be unambiguous,__str__
should be readablecopy
anddeepcopy
functions work on arbitrary objects (including custom classes)__repr__
method, named access, and iterability, and reasonably nice definition syntax.update
, and creation with defaults{'default': 3, **override}
The text was updated successfully, but these errors were encountered: