Python Special Attributes

Python has multiple special attributes that are defined per default for each class such as __name__, __module__, __dict__, __bases__, __doc__, and __annotations__. Each of these special attributes has a special meaning as shown in the following table: Attribute Type Description __name__ str The name of the class __module__ str The string name of the module … Read more

Python __new__ Magic Method

Python’s __new__(cls) magic method creates a new instance of class cls. The remaining arguments are passed to the object constructor. The return value is the newly-created object—an instance of cls. Basic Example The following example shows how each time you create an object of our custom class My_Class, Python calls the __new__() magic method. Output: … Read more

How to Find Out if an Ethereum Address is a Contract?

Ethereum blockchain proposed tamper-proof decentralized financial contracts and applications. One of the main objectives of Ethereum is constructing decentralized applications that run without any downtime, fraud, control, or interference from a third party. Since it does not allow third-party interference, it is literally unthinkable to find a way to check if the address is a … Read more

Cómo agregar datos a un archivo JSON en Python [+Vídeo]

Switch to English Version Planteamiento del problema Dado un objeto JSON almacenado en un archivo llamado “your_file.json”, como una lista de diccionarios. ¿Cómo se pueden anexar datos, como un nuevo diccionario? # File “your_file.json” (BEFORE) [{“alice”: 24, “bob”: 27}] # New entry: {“carl”: 33} # File “your_file.json” (AFTER) [{“alice”: 24, “bob”: 27}, {“carl”: 33}] Método … Read more

Pandas merge_ordered() – A Simple Guide with Video

In this tutorial, we will learn about the Pandas function merge_ordered(). This method performs a merge with optional interpolation. It is especially useful for ordered data like time series data. Syntax and Parameters Here are the parameters from the official documentation: Parameter Type Description left DataFrame right DataFrame on label or list Field names to … Read more

How to Calculate the Edit Distance in Python?

Motivation Type “helo world” into your Google search bar and Google will ask you: “Did you mean: hello world”. How is this done? A simple method to detect these typos is the Levenshtein distance (also called edit distance). In fact, Google’s algorithm seems to use some variant of it. (source) By studying this article, you’ll … 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

­Pandas DataFrame describe(), diff(), eval(), kurtosis()

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

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