frozen=True: Make Your Data Classes Read-Only
If you don’t want anybody to adjust the attributes of a class, use @dataclass(frozen=True).
In the example above, changing the attribute color of the DataClassDog‘s instance will throw an error.
If you don’t want anybody to adjust the attributes of a class, use @dataclass(frozen=True).
In the example above, changing the attribute color of the DataClassDog‘s instance will throw an error.
Sometimes your list of dictionaries can be nested like above. How can you get the second price of each apple?
That is when the map_ method of pydash comes in handy. Pydash is the kitchen sink of Python utility libraries for doing “stuff” in a functional way.
pydash._map: Get Values from a Nested List of Dictionaries Read More »
Lazy evaluation is an evaluation strategy that holds the evaluation of an expression until its value is needed. This method avoids repeated evaluation and discards sub-expressions that are not directly linked to the final result of the expression.
To use lazy evaluation in Python, use pydash’s method chaining. pydash’s method chaining also increases the readability of your code when applying multiple methods to a Python object.
Play with pydash in this Google Colab notebook.
Have you ever found it lengthy to use brackets to access the elements in a deeply nested dictionary? If so, use pydash.get.
With pydash.get, you can use a dot notation with a dictionary like above.
typing is a Python module that allows developers to specify the types of inputs to make sure the input types are correct. To specify an input is of type function, use typing.Callable.
Callable can be used static type checker such as mypy to check if the input is indeed a function.
typing.Callable: Specify an Input is of Type Function Read More »
Do you find it annoying to write an __init__ method every time you want to create a class in Python? If so, try attrs.
attrs allows you to:
declaratively define the attributes of a classget a nice human-readable __repr__ without extra codeturn attributes of an instance into a dictionary
Find other benefits of using attrs here.
If you want to group elements in a list by a key, use itertools.groupby. In the example above, I grouped elements in the list by the first element in each tuple.
itertools.groupby: Group Elements in an Iterable by a Key Read More »
Normally, to access multiple indices from a list, you need to use list comprehension. To do the same thing with simpler syntax, use operator.itemgetter instead.
operator.itemgetter: Get Multiple Items From a List or Dictionary Read More »
Normally, you cannot filter a list using a list. To filter a list using a list of booleans, use itertools.compress instead.
itertools.compress: Filter a List Using Booleans Read More »
map is a useful method that allows you to apply a function to elements in a list. However, it can’t apply a function with more than one argument to a list.
To apply a function with more than 2 arguments to elements in a list, use itertools.starmap. With starmap, elements in each tuple of the list nums are used as arguments for the function multiply.
Link to my previous tips about itertools.
itertools.starmap: Apply a Function With More Than 2 Arguments to Elements in a List Read More »