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

How to Visualize Data in Python — Graphviz, ImageDraw, and Turtle

“Numbers have an important story to tell. They rely on you to give them a clear and convincing voice.” ― Stephen Few “Visualizations act as a campfire around which we gather to tell stories.” ― Al Shalloway “The goal is to turn data into information and information into insight.” ― Carly Fiorina These quotes are … 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

How to Show Images in Python

Showing pictures in Python This post will show different ways of displaying pictures in Python. The options we will explore are: PIL (Python Image Library) OpenCV (Computer Vision Library) IPython Matplotlib Library Method 1: PIL (Python Image Library) PIL is the standard library to manage images in Python. They discontinued the project in 2011, but … 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

­Pandas Methods count(), cov() & cumX()

The Pandas DataFrame has several methods concerning Computations and Descriptive Stats. When applied to a DataFrame, these methods evaluate the elements and return the results. Preparation Before any data manipulation can occur, two (2) new libraries will require installation. The Pandas library enables access to/from a DataFrame. The NumPy library supports multi-dimensional arrays and matrices … Read more

Duck Typing in Python

💡 Idea: Duck typing is a computer science concept where the type of an object is largely ignored—only the methods the object defines matter. This is sometimes referred to as dynamic typing because the type of an object is determined dynamically at runtime rather than checked by the compiler. Instead of checking the type, the … Read more