Object Orientation

Python’s __import__() Function — Dynamically Importing a Library By Name

Python’s built-in “dunder” function __import__() allows you to import a library by name. For example, you may want to import a library that was provided as a user input, so you may only have the string name of the library. For example, to import the NumPy library dynamically, you could run __import__(‘numpy’). In this tutorial, …

Python’s __import__() Function — Dynamically Importing a Library By Name Read More »

__getattr__ vs. __getattribute__ — What’s the Difference?

As a programmer, you generally appreciate more control of your data and more control on the behavior of my program itself. It is in this moment when Python comes in our help with all its power letting us get right into the flow of our programming, avoiding us to lose something like define an attribute …

__getattr__ vs. __getattribute__ — What’s the Difference? Read More »

Python getattr()

Python’s built-in getattr(object, string) function returns the value of the object‘s attribute with name string. If this doesn’t exist, it returns the value provided as an optional third default argument. If that doesn’t exist either, it raises an AttributeError. An example is getattr(porsche, ‘speed’) which is equivalent to porsche.speed. Usage Learn by example! Here’s an …

Python getattr() Read More »

Python setattr()

Python’s built-in setattr(object, string, value) function takes three arguments: an object, a string, and an arbitrary value. It sets the attribute given by the string on the object to the specified value. After calling the function, there’s a new or updated attribute at the given instance, named and valued as provided in the arguments. For …

Python setattr() Read More »

Python delattr()

Python’s built-in delattr() function takes an object and an attribute name as arguments and removes the attribute from the object. The call delattr(object, ‘attribute’) is semantically identical to del object.attribute. This article shows you how to use Python’s built-in delattr() function. Usage Learn by example! Here’s an example on how to use the delattr() built-in …

Python delattr() Read More »

Python staticmethod()

Static methods are special cases of class methods. They’re bound to a class rather than an instance, so they’re independent on any instance’s state. Python’s built-in function staticmethod() prefixes a method definition as an annotation @staticmethod. This annotation transforms a normal instance method into a static method. The difference between static (class) methods and instance …

Python staticmethod() Read More »

Python classmethod()

Python’s built-in function classmethod() prefixes a method definition in a class as an annotation @classmethod. This annotation transforms a normal instance method into a class method. The difference between class and instance method is that Python passes the class itself as a first implicit argument of the method rather than the instance on which it …

Python classmethod() Read More »

What’s the Most Pythonic Way to Alias Method Names?

In contrast to a normal Python method, an alias method accesses an original method via a different name—mostly for programming convenience. An example is the iterable method __next__() that can also be accessed with next(). You can define your own alias method by adding the statement a = b to your class definition. This creates …

What’s the Most Pythonic Way to Alias Method Names? Read More »

How to Assign a Function to a Variable in Python?

Challenge: Given is function f. How to assign the function to variable g, so that you can call g() and it runs function f()? Your desired output is function f‘s output: How to accomplish this in the most Pythonic way? Overview: We examine two methods to accomplish this challenge. You can run them in our …

How to Assign a Function to a Variable in Python? Read More »