Name Mangling

Name mangling is a feature in Python that is used to avoid naming conflicts in class hierarchies by adding a prefix to the name of a class member that starts with two underscores, but does not end with more than one underscore. When a member name is prefixed with two underscores, Python automatically rewrites the name in a way that makes it harder to access the member from outside the class....

April 10, 2023 · aaron

Monkey Patching

In Python programming, monkey patching refers to the practice of modifying or extending the behavior of a module or object at runtime. This technique allows developers to change the behavior of existing code without modifying the original source code. While monkey patching can be a powerful tool, it can also be dangerous if used improperly. What is Monkey Patching? Monkey patching is a technique that allows developers to dynamically modify the behavior of an object or module at runtime....

April 9, 2023 · aaron

Itertools

itertools is a powerful Python library that provides a collection of tools for working with iterators and iterable objects. The library is part of the Python standard library, which means that it comes pre-installed with every Python distribution. itertools provides a suite of functions that can help you manipulate and iterate over iterable objects in a more efficient and concise manner. In this article, we’ll explore some of the most useful functions in itertools and give some funny examples to help you understand when to use them....

April 8, 2023 · aaron

Generator

In Python, a generator is a special type of function that can be used to create iterable sequences of values on-the-fly. Unlike regular functions, which compute and return a value immediately, generators can generate a sequence of values over time, using the yield keyword. Example: The following fib function is a generator function that generates a sequence of Fibonacci numbers up to a given limit index n. def fib(n): a = [0, 1] i = 0 while i <= n: yield a[i%2] a[i%2] = a[0] + a[1] i += 1 The yield keyword is used to return each generated Fibonacci number as it is generated, which makes the function a generator....

April 7, 2023 · aaron

Duck Typing

Duck typing is a concept in dynamic programming languages like Python, where the type of an object is determined not by its class name, but by its behavior or the methods and attributes it defines. The term “duck typing” comes from the phrase “if it looks like a duck, swims like a duck, and quacks like a duck, then it probably is a duck.” In Python, duck typing means that an object’s suitability for a given task is determined by the presence of specific methods or attributes, rather than its type....

April 6, 2023 · aaron