Python __import__() Magic Method

πŸ›‘ Overriding this function is strongly discouraged. To change the semantics of the import statement, use import hooks instead! Still here? πŸ˜‰ So, let’s get started learning the syntax of this function. You can also check out our in-depth article on the __import__ statement here. Syntax __import__(name, globals=None, locals=None, fromlist=(), level=0) Parameter Description name Import … Read more

Python __instancecheck__() Magic Method

Syntax class.__instancecheck__(self, instance) Python’s class.__instancecheck__(self, instance) method implements the isinstance(instance, class) built-in function. It should return True if instance is a direct or indirect instantiated object of class (e.g., via Python inheritance). We call this a “Dunder Method” for “Double Underscore Method” (also called “magic method”). To get a list of all dunder methods with … Read more

Iterative vs. Recursive Binary Search Algorithms in Python

In this article, you’ll learn about a basic algorithm, every computer scientist must know: the binary search algorithm. I have drawn the code from my NoStarch programming introductory book Python One-Liners: Applications Binary Search The algorithm has important practical applications in many basic data structures such as sets, trees, dictionaries, bags, bag trees, bag dictionaries, … Read more

Python __subclasscheck__() Magic Method

Syntax class.__subclasscheck__(self, subclass) Python’s class.__subclasscheck__(self, subclass) method implements the issubclass(subclass, class) built-in function. It should return True if subclass is a direct or indirect subclass of class using Python inheritance to define the subclass relationship. We call this a “Dunder Method” for “Double Underscore Method” (also called “magic method”). To get a list of all … Read more

Python __sizeof__() Method

Syntax object.__sizeof__(self, other) The Python __sizeof__() method returns the size of the object in bytes. The sys.getsizeof() method internally call’s __sizeof__() and adds some additional byte overhead, e.g., for garbage collection. Basic Example The following code snippet creates a list object x with three integers and measures its size in bytes (104) using the x.__sizeof__() … Read more

Python __contains__() Magic Method

Syntax and Definition The Python __contains__() magic method implements the membership operation, i.e., the in keyword. Semantically, the method returns True if the argument object exists in the sequence on which it is called, and False otherwise. For example, 3 in [1, 2, 3] returns True as defined by the list method [1, 2, 3].__contains__(3). … Read more

Python Nested Multiple Ternary Operators

In which order does the nested ternary operator evaluate its conditions? Short Answer: The nested ternary operator ‘1’ if x else ‘2’ if y else ‘3’ evaluates the condition from left to right, i.e., ‘1’ if x else (‘2’ if y else ‘3’). In short, first condition first! Problem Formulation Given a nested ternary operator … Read more

Python __repr__() Magic Method

Syntax object.__repr__(self) The Python __repr__ method returns a string representation of the object on which it is called. It implements the built-in repr() function. If you call print(x) an object x, Python internally calls x.__str__() to determine the string representation of object x. If this isn’t implemented, Python calls x.__repr__(). We call this a “Dunder … Read more

Do You Need to Escape a Dot in a Python Regex Character Class?

Question Recap the following regular expression concepts (more details in the article below): The dot character . in regular expressions matches any character excluding the newline character. For example, the pattern ‘c.t’ will match the strings ‘cat’, ‘cut’, or ‘czt’. The character class [ ] is a set of characters: if you use it in … Read more

Python __dir__() Magic Method

Python’s __dir__() magic method implements the functionality of the dir() built-in function. Semantically, dir() returns all (function, object, or variable) names in a given scope. However, the magic method __dir__() converts any return value to a sorted list. Minimal Example The following code defines a custom class My_Class and overrides the __dir__() magic method to … Read more