(Solved) Python TypeError: ‘float’ object is not subscriptable

Problem Formulation Consider the following minimal example where a TypeError: ‘float’ object is not subscriptable occurs: This yields the following output: Solution Overview Python raises the TypeError: ‘float’ object is not subscriptable if you use indexing or slicing with the square bracket notation on a float variable that is not indexable. However, the float class … Read more

Python Package Version: the __version__ Attribute

Python __version__ Attribute Python contains many “Magic Methods/Attributes”. One of these is __version__ commonly called “Dunder version” because of the double underscore before and after version. In this article, I will briefly look at what a Dunder Method/Attribute is and talk about __version__. What Does a Dunder Method/Attribute Do? Dunder Methods/Attributes, also called “Magic Methods” … Read more

How to Fix TypeError: unhashable type: ‘list’

The TypeError: unhashable type: ‘list’ usually occurs when you try to use a list object as a set element or dictionary key and Python internally passes the unhashable list into the hash() function. But as lists are mutable objects, they do not have a fixed hash value. The easiest way to fix this error is … Read more

Python __aiter__() and __anext__() Magic Methods

object.__aiter__(self) object.__anext__(self) 💡 Summary: Python’s __aiter__() and __anext__() methods are used to implement an asynchronous for loop (keywords: async for). In contrast to a normal (synchronous) for loop, an asynchronous for loop iterates over an asynchronous source. __aiter__() returns an asynchronous iterator object (in many cases it’s simply a reference to itself: return self) __anext__() … Read more

Python __aexit__() Magic Method

object.__aexit__(self, exc_type, exc_val, exc_tb) 💡 Summary: Python’s __aexit__() magic method is semantically similar to __exit__() but is used for asynchronous and parallel programming. Python calls the __aexit__() magic method when leaving an async with block whereas the __aenter__() method is called when entering it. An object that implements both __aenter__() and __aexit__() methods is called … Read more

Python __aenter__() Magic Method

object.__aenter__(self) 💡 Summary: Python’s __aenter__() magic method is semantically identical to __enter__() but is used for asynchronous and parallel programming. Python calls the __aenter__() magic method when starting an async with block whereas the __aexit__() method is called when leaving it. An object that implements both __aenter__() and __aexit__() methods is called an asynchronous context … Read more

Python __exit__() Magic Method

object.__exit__(self, exc_type, exc_value, traceback) 💡 Summary: Python calls the __exit__() magic method when ending a with block whereas the __enter__() method is called at the start. An object that implements both __exit__() and __enter__() is called a context manager. By defining those methods, you can create your own context manager. We define a custom class … Read more

Python __enter__() Magic Method

object.__enter__(self) 💡 Summary: Python calls the __enter__() magic method when starting a with block whereas the __exit__() method is called at the end. An object that implements both __enter__() and __exit__() methods is called a context manager. By defining those methods, you can create your own context manager. We define a custom class MySecretConnection. This … Read more

Python __format__() Magic Method

Syntax object.__format__(self, spec) The Python __format__() method implements the built-in format() function as well as the string.format() method. So, when you call format(x, spec) or string.format(spec), Python attempts to call x.__format__(spec). The return value is a string. We call this a “Dunder Method” for “Double Underscore Method” (also called “magic method”). To get a list … Read more

Python __set_name__() Magic Method

Python’s magic method obj.__set_name__(self, owner, name) method is created on an attribute obj when the class owner holding the attribute is created. We call this a “Dunder Method” for “Double Underscore Method” (also called “magic method”). To get a list of all dunder methods with explanation, check out our dunder cheat sheet article on this … Read more

Python __set__ Magic Method

Python’s __set__() magic method sets a given attribute on an instance of a class holding the attribute (=owner class) to a new value. When setting the attribute through the owner class, Python dynamically executes the attribute’s __set__() method to override its value on the given instance argument. For example, if you create a class Person … Read more