dir() versus __dir__() – What’s the Difference?

Problem Formulation What’s the difference between the built-in dir() function and __dir__ dunder method in Python? Quick Answer Python’s built-in function dir(object) returns a list of the object’s attribute names and method names. The dir() function is a wrapper around the __dir__() method because it internally calls the object’s dunder method object.__dir__(). But the two … Read more

How to Restart a Loop in Python?

Problem Formulation Given a Python loop and a logical condition. How to restart the loop if the condition is met? Solution 1: Reset While Loop The while loop checks a condition in order to determine whether the loop body should be executed or not. You can reset the condition to the initial value in order … Read more

How to Convert a String to a Dictionary in Python?

Problem Formulation Given a string representation of a dictionary. How to convert it to a dictionary? Input: ‘{“1”: “hi”, “2”: “alice”, “3”: “python”}’ Output: {‘a’: 1, ‘b’: 2, ‘c’: 3} Method 1: eval() The built-in eval() function takes a string argument, parses it as if it was a code expression, and evaluates the expression. If … Read more

How to Convert a Unicode String to a Dictionary in Python?

Problem Formulation Given a Unicode string representation of a dictionary. How to convert it to a dictionary? Input: u”{‘a’: 1, ‘b’: 2, ‘c’: 3}” Output: {‘a’: 1, ‘b’: 2, ‘c’: 3} Note: The u’string’ representation represents a Unicode string that was introduced in Python 3. This is redundant as all strings in Python 3 are … Read more

How to Log a Python Error with Debug Information?

[toc] Generally, logging is an extremely valuable tool in a software developer’s toolbox. Logging helps to assist you with the flow of a program and find situations that you probably would not have considered while coding. By logging useful data and information, you can troubleshoot the errors effectively as well as utilize the information to … Read more

Python List of Dunder Methods

When searching for a list of dunder methods, I only found a couple of resources all over the web—each covering only a fraction of dunder methods. So, here’s my collection of 127 dunder methods (also called “magic methods”) from those sources, sorted alphabetically: __abs__ __add__ __aenter__ __aexit__ __aiter__ __and__ __anext__ __await__ __bool__ __bytes__ __call__ __ceil__ … Read more

Python Version Backward Compatibility

The Python language does not generally provide backward compatibility. The ability to break inefficiencies and fix wrong design choices are major reasons why Python has remained lean and efficient over the last decades. However, the PEP 387 standard discusses that incompatibility issues should be well thought out. Here’s the basic policy when backward incompatibilities may … Read more

Pytest – A Complete Overview

Pytest is a popular test framework in Python. It helps to automate the test execution process and run unit tests as frequently as possible with minimal effort.  Everyone who has at least some experience with computer programming would intuitively know that testing is critical when building a software application. But beginners often find it difficult … Read more