Python Single vs Double Underscore (_ vs __)

The single underscore in Python “_” is used to either make a variable different from a Python keyword such as in float_=8, or to indicate that it should be used in a private context such as in _var=8. The double underscore in Python “__” (called “dunder“) is used to make an instance attribute or method … Read more

Python Class vs Instance Attributes [Tutorial+Video]

A Python class attribute is a variable that belongs to a class, not an individual instance, and is shared among all instances. An instance attribute is a variable that belongs to exactly one object. An instance attribute is defined inside the __init__() method by using the reference to the instance self, whereas a class attribute … Read more

A Simple Example of Python Objects and Classes [+Video]

In Python, everything is an object. Even integers are objects — this is different from programming languages like Java where integers, floats, and Booleans are primitive data types. In this way, Python is built on a rigorously consistent object-oriented paradigm. Considering that objects are everywhere in Python, object-oriented programming is not well studied by Python … Read more

Static and Dynamic Attributes in Python – What’s the Difference?

Quick Answer: Static attributes are variables defined once for the class and shared by all instances. Dynamic attributes are variables defined for individual instances only. Static variables are used as a “fall-back” when there are no explicit dynamic attributes defined for the instances. When you try to “overwrite” a static attribute such as in x.attr … Read more

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 … 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 … Read more

How to Call a Parent’s Class Method in Python?

Problem Formulation: Calling Parent Methods Given Parent class P Child class C that inherits from P Class method P.my_method() defined on the parent P Problem: Within a method of class C, how to call the parents method P.my_method()? Example: Consider the following example where you create a parent and a child class. You want to … 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 … 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 … 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 … Read more