Factory Method Pattern

“Define an interface for creating an object, but let subclasses decide which class to instantiate. The Factory method lets a class defer instantiation it uses to subclasses.” (Gang Of Four) The Factory Method design pattern is a creational design pattern that provides an interface for creating objects in a superclass, but allows subclasses to alter the type of objects that will be created. In other words, it encapsulates object creation and delegates it to subclasses, rather than instantiating objects directly in the superclass....

May 20, 2023 · aaron

Overriding vs. Overloading

Both overriding and overloading are concepts in object-oriented programming that allow you to define methods with the same name but different behavior. However, they differ in their implementation and purpose. Overriding Overriding is a mechanism by which a subclass can provide a different implementation of a method that is already defined in its superclass. When a method is overridden, the subclass’s implementation takes precedence over the superclass’s implementation, allowing you to customize the behavior of the method for the specific subclass....

April 12, 2023 · aaron

Namedtuple

The namedtuple function is a factory function from the collections module in Python that creates a new class-like object that behaves like a tuple, but with named fields. Here are some cases where you might consider using namedtuple: Improved readability: If you have a collection of records or objects that have a fixed set of attributes, using a namedtuple can make your code more readable by giving names to the fields instead of relying on integer indices....

April 11, 2023 · aaron

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