Object Orientation

How to Dynamically Create a Function in Python?

Problem Formulation There are different variants of this problem that all ask the same thing: How to create a function dynamically in Python? How to define a function at runtime? How to define a function programmatically? How to create a function from a string? There are many ways to answer these questions—most web resources provide …

How to Dynamically Create a Function in Python? Read More »

How to Determine the Type of an Object in Python?

Problem Formulation Every Python object is of a certain type, also called “class”. The class is a blueprint that shows the data and capabilities of each object/instance that is created after this blueprint. Given a Python object (=instance). How to determine/check/get its type (=class)? There are many variants of this question: How to determine the …

How to Determine the Type of an Object in Python? Read More »

Python super() – A Simple Illustrated Guide

Python’s built-in super() method returns a temporary object of the superclass to help you access its methods. Its purpose is to avoid using the base class name explicitly. It also enables your class to inherit from multiple base classes. Visual Idea super() The idea is simple: use super() to call the methods defined in the …

Python super() – A Simple Illustrated Guide Read More »

Python property() — What You Always Wanted to Know But Never Dared to Ask

Object-orientation is great way to encapsulate data in your application. This minimizes complexity and adheres to good software engineering principles. However, attributes in Python can be easily accessed from the outside—they’re not really encapsulated. That’s one of the reason the property() built-in function exists: it allows you to truly encapsulate data with the means of …

Python property() — What You Always Wanted to Know But Never Dared to Ask Read More »

Python vars() Function

Python’s built-in vars() function returns the __dict__ attribute of an object—a dictionary containing the object’s changeable attributes. Without argument, it returns the local symbol table similar to locals(). Python’s built-in vars() function returns a dictionary of name: value mappings of all the names defined in the local scope or the scope of the optional object …

Python vars() Function Read More »

Python object() Function

Python’s built-in object() function takes no argument and returns a new featureless object—the base and parent of all classes. As such it provides all methods that are common to all Python class instances such as __repr__() and other “dunder” methods. However, unlike for all non-object instances, you cannot assign arbitrary attributes to an instance of …

Python object() Function Read More »

Python repr() Function — A Helpful Guide with Example

Python’s built-in repr(obj) function returns the standard string representation of the provided object. This often includes the type and memory address of the object—for lack of further information. For example, the returned string representation may be ‘<main.Car object at 0x000001F66D11DBE0>’ for a custom object of type Car. The function internally calls the method obj.__repr__() which …

Python repr() Function — A Helpful Guide with Example Read More »

Python str() Function

Python’s built-in str(x) function converts the object x to a string using the x.__str__() method or, if non-existent, the repr(x) built-in function to obtain the string conversion. Syntax str() Syntax: str(object) # –> Most common case: convert an object to a string str(object=b”, encoding=’utf-8′, errors=’strict’) # –> Not so common case: Converts a bytes or …

Python str() Function Read More »