The Shortest Quicksort Algorithm in Python

Quicksort is not only a popular question in many code interviews – asked by Google, Facebook, and Amazon – but also a practical sorting algorithm that is fast, concise, and readable. Because of its beauty, you won’t find many introductions to algorithms that don’t discuss the Quicksort algorithm. In this one-liner tutorial, you’ll learn about … Read more

Python __trunc__() Magic Method

Syntax and Definition object.__trunc__(self) The Python __trunc__() method implements the behavior of the math.trunc() function. For example, if you attempt to call math.trunc(x), Python will run the x.__trunc__() method to obtain the return value. We call this a “Dunder Method” for “Double Underscore Method” (also called “magic method”). To get a list of all dunder … Read more

Python __floor__() Magic Method

Syntax and Definition object.__floor__(self) The Python __floor__() method implements the behavior of the math.floor() function. For example, if you attempt to call math.floor(x), Python will run the x.__floor__() method to obtain the return value. We call this a “Dunder Method” for “Double Underscore Method” (also called “magic method”). To get a list of all dunder … Read more

Python __ceil__() Magic Method

Syntax and Description object.__ceil__(self) The Python __ceil__() method implements the behavior of the math.ceil() function. For example, if you attempt to call math.ceil(x), Python will run the x.__ceil__() method to obtain the return value. We call this a “Dunder Method” for “Double Underscore Method” (also called “magic method”). To get a list of all dunder … Read more

Python __round__() Magic Method

Syntax object.__round__(self, ndigits=0) The Python __round__() method implements the built-in round() function. For example, if you attempt to call round(x) or round(x, ndigits), Python will run the x.__round__() or x.__round__(ndigits) method, respectively. The following code snippet overrides the __round__() dunder method to return the rounded age of a Person when you pass an object of … Read more

Python __rdiv__ Magic Method

The Python __rdiv__() magic method overrides the reverse division operation for a custom object in Python 2. In Python 3, it was replaced by the __rtruediv__() and __rfloordiv__() dunder methods. The Python __rtruediv__() method is called to implement the normal division operation / called true division and apply it in reverse. The Python __rfloordiv__() method … Read more

Python __neg__ Magic Method

To customize the behavior of the negation operator -x, override the __neg__(self) dunder method in your class definition. Python internally calls x.__neg__() to calculate the inverse (negation) of an object, i.e., -x. If the __neg__() method is not defined, Python will raise a TypeError. Syntax __neg__(self) To use the negation operator -x on a custom … Read more

How to Override the “not” Operator in a Python Magic Method?

Short Answer: To override the logical not operator for a custom Python class My_Class, redefine the dunder method My_Class.__bool__() to return your custom Boolean value. This ensures that bool(x) on a My_Class object x returns either True or False. The operation not x will then return the inverse Boolean value, i.e, not x evaluates to … Read more

Python __iter__() Magic Method

Syntax object.__iter__(self) The Python __iter__ method returns an iterator object. An iterator object is an object that implements the __next__() dunder method that returns the next element of the iterable object and raises a StopIteration error if the iteration is done. Formally, the __iter__() method implements the built-in iter() function. For example, if you call … Read more

Python __next__() Magic Method

Syntax object.__next__(self) The Python __next__ method returns an arbitrary element that represents the “next” element when you iterate over the object on which it is called. For example, if you iterate over my_object using for x in my_object, Python internally calls my_object.__next__() in each loop iteration to determine the next element. Formally, the __next__() method … Read more