Basics

Property

TL;DR Use @property decorator for getters and setter in OOP in a more pythonic way. Python programming provides us with a built-in @property decorator which makes usage of getter and setters much easier in Object-Oriented Programming.

2023-02-12

Import

In Python, we use the import keyword to make code in one module available in another. Imports in Python are important for structuring your code effectively. Using imports properly will make you more productive, allowing you to reuse code while keeping your projects maintainable.

2023-02-11

Dictionary Tricks

Sorting Dictionary Python dictionaries don’t have an inherent order. You can iterate over them just fine but there’s no guarantee that iteration returns the dictionary’s elements in any particular order。

2023-02-04

Generator

In Python, a generator is a function that returns an iterator that produces a sequence of values when iterated over. Generators are useful when we want to produce a large sequence of values, but we don’t want to store all of them in memory at once.

2023-01-20

Looping and Iterations

TL;DR Iterators provide a sequence interface to Python objects that’s memory efficient and considered Pythonic. To support iteration an object needs to implement the iterator protocol by providing the __iter__ and __next__ dunder methods.

2023-01-14

Priority Queues

TL;DR queue.PriorityQueue stands out from the pack with a nice object-oriented interface and a name that clearly states its in- tent. It should be your preferred choice. If you’d like to avoid the locking overhead of queue.

2023-01-08

Queues

TL;DR collections.deque is an excellent default choice for implementing a FIFO queue data structure in Python list objects can be used as queues, but this is generally NOT recommended due to slow performance.

2023-01-08

Stacks

TL;DR collections.deque provides a safe and fast general-purpose stack implementation. First choice! The built-in list type can be used as a stack, but be careful to only append and remove items with append() and pop() in order to avoid slow performance.

2023-01-08

Sets

TL;DR Use the built-in set type when looking for a mutable set. frozenset objects are hashable and can be used as dictionary or set keys. collections.Counter implements multiset or “bag” data structures.

2023-01-07

Records, Structs, and Data Transfer Objects

TL;DR You only have a few (2-3) fields $\rightarrow$ Using a plain tuple object may be okay if the field order is easy to remember or field names are superfluous. You need immutable fields $\rightarrow$ plain tuples, collections.

2023-01-05