Labs / Python OOP

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 44 Python Built-in functions and Magic Methods – Overriding Magic Methods of Built-in Types

Author: Li Zisheng · Source: FreakStudio open documentation · License: CC BY 4.0

Contents
The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 44 Python Built-in functions and Magic Methods – Overriding Magic Methods of Built-in Types

Abstract:

In Python, the behavior of built-in types is implemented through a set of special "magic methods", which start and end with double underscores, such as `init` and `str`; you can customize or extend the behavior of built-in types by overriding these magic methods.

FreakStudio's Blog

Previous Recommendations:

You who are learning embedded systems, don’t you still know Object Oriented yet? ?!

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 00 Introduction to Object Oriented Design Methodology

The Most Beginner-Friendly Object Oriented Programming Course on the Whole Network: 01 Basic Concepts of Object Oriented Programming

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 02 Python Implementation of Classes and Objects - Creating Classes with Python

The Most Beginner-Friendly Object Oriented Programming Course on the Whole Network: 03 Python Implementation of Classes and Objects – Adding Attributes to Custom Classes

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 04 Python Implementation of Classes and Objects – Adding Methods to Custom Classes

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 05 Python Implementation of Classes and Objects - PyCharm Code Tab

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 06 Python Implementation of Classes and Objects - Data Encapsulation of Custom Classes

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 07 Python Implementation of Classes and Objects - Type Annotations

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 08 Python Implementation of Classes and Objects - @property Decorator

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 09 Python Implementation of Classes and Objects - Relationships Between Classes

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 10 Python Implementation of Classes and Objects - Class Inheritance and Liskov Substitution Principle

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 11 Python Implementation of Classes and Objects - Subclass Calling Parent Class Methods

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 12 Python Implementation of Classes and Objects - Using Python's logging Module to Output Program Runtime Logs

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 13 Python Implementation of Classes and Objects – Installation and Usage of Sourcetrail, a Visual Code Reading Tool

The Most Beginner-Friendly Object Oriented Programming Course on the Whole Network: 14 Python Implementation of Classes and Objects – Static Methods and Class Methods of Classes

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 15 Python Implementation of Classes and Objects - the __slots__ Magic Method

The Most Beginner-Friendly Object Oriented Programming Tutorial Across the Network: 16 Python Implementation of Classes and Objects - Polymorphism, Method Overriding and Open Closed Principle

The most beginner-friendly Object Oriented programming tutorial on the entire network: 17 Python implementation of classes and objects - duck typing and "file-like object"

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 18 Python Implementation of Classes and Objects - Multiple Inheritance and Serial Port Data Curve Plotting with PyQtGraph

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 19 Python Implementation of Classes and Objects – Using PyCharm to Automatically Generate File Comments and function Comments

The Most Beginner-Friendly Object Oriented Programming Course on the Whole Network: 20 Python Implementation of Classes and Objects – Implementation of Composition Relationship and CSV File Saving

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 21 Python Implementation of Classes and Objects - Multi-File Organization: Module and Package

The Most Beginner-Friendly Object Oriented Programming Course on the Whole Network: 22 Python Implementation of Classes and Objects - Exceptions and Syntax Errors

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 23 Python Implementation of Classes and Objects - Throwing Exceptions

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 24 Python Implementation of Classes and Objects - Exception Catching and Handling

The Most Beginner-Friendly Object Oriented Programming Course on the Whole Network: 25 Python Implementation of Classes and Objects - Python Determines the Type of Input Data

The Most Beginner-Friendly Object Oriented Programming Tutorial Across the Network: 26 Python Implementation of Classes and Objects - Context Managers and the with Statement

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 27 Python Implementation of Classes and Objects - Exception Hierarchy and Custom Exception Class Implementation in Python

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 28 Python Implementation of Classes and Objects - A Comprehensive Summary of Python Programming Principles, Philosophy and Specifications

The Most Beginner-Friendly Object Oriented Programming Course on the Whole Network: 29 Python Implementation of Classes and Objects - Assertions, Defensive Programming and the Usage of the help function

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 30 Python's Built-in Data Types - The object Root Class

The most beginner-friendly Object Oriented programming tutorial across the entire network: 31 Built-in data types in Python - Object and Type

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 32 Python's Built-in Data Types - Class and Instance

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 33 Python's Built-in Data Types – The Relationship Between Object and Type

The Most Beginner-Friendly Object Oriented Programming Course on the Whole Network: 34 Python's Built-in Data Types - Common Composite Data Types in Python: Tuples and Named Tuples

The Most Beginner-Friendly Object Oriented Programming Tutorial Across the Web: 35 Python's Built-in Data Types – Docstrings and the __doc__ Attribute

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 36 Python's Built-in Data Type - Dictionary

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 37 Common Composite Data Types in Python - Lists and List Comprehensions

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 38 Common Composite Data Types in Python – Implementing stack, Queue and Deque with Lists

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 39 Common Composite Data Types in Python – Sets

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 40 Common Composite Data Types in Python - Enumerations and Usage of the enum Module

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 41 Common Composite Data Types in Python - Queues (FIFO, LIFO, Priority Queue, Deque and Circular Queue)

The Most Beginner-Friendly Object Oriented Programming Tutorial Across the Network: 42 Commonly Used Composite Data Types in Python - Collections Container Data Types

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 43 Common Composite Data Types in Python - Extending Built-in Data Types

For more exciting content, you can check out:

Speed Up Your Python: A Quick Guide to Python Parallel Computing

Understand the Debugging Principle of CM3 Microcontrollers in One Article

After half a month of dedicated grinding, the ultimate summary of embedded technology stack is now released

The "Secret Martial Arts Manual" for Computer Science Competitions

Avnet ZUBoard 1CG Development Board — A New Choice for Deep learning

SenseCraft deploys models to the Grove Vision AI V2 image processing module

Document and Code Acquisition:

You can download the document via the following link:

https://github.com/leezisheng/Doc

19

This document mainly introduces how to use Python for Object Oriented programming, which requires readers to have a basic understanding of Python syntax and microcontroller development. Compared with other blogs or books that explain Python Object Oriented programming, this document is more detailed and focuses on embedded host computer applications, taking the common serial port data transmission and reception, data processing, dynamic graph drawing and other functions between the host computer and the slave computer as application examples. Meanwhile, it uses the Sourcetrail code software to visualize the code for reading to facilitate readers' understanding.

The link to obtain the relevant sample code is as follows:

https://github.com/leezisheng/Python-OOP-Demo

Body Text

Python Built-in Functions and Magic Methods

In Python, there are many functions that can perform specific tasks or compute results for objects of specific types, and these functions do not need to be methods of some underlying class. They are usually abstracted common computations that can be applied to classes of multiple types. These functions are collectively called built-in functions, and we often use built-in functions to call the magic methods of objects. The so-called Magic Method is a built-in method of Python, which starts and ends with two underscores. It does not need to be called actively, and exists for the purpose of being called by the Python interpreter.The common built-in functions are as follows:

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 44 Python Built-in functions and Magic Methods – Overriding Magic Methods of Built-in Types

The simplest example is the built-in len () function, which counts the number of items in a container object such as a dictionary or a list. In fact, a list object does not have a len length attribute. You may not believe that, so let's check the attribute values of the list. Enter the following code:

print(list.__dict__)

Let's take a look at the output:

{'__new__': <built-in method __new__ of type object at 0x00007FF9D4D5B6D0>,
 '__repr__': <slot wrapper '__repr__' of 'list' objects>, '__hash__': None, 
'__getattribute__': <slot wrapper '__getattribute__' of 'list' objects>, 
'__lt__': <slot wrapper '__lt__' of 'list' objects>, '__le__': <slot wrapper 
'__le__' of 'list' objects>, '__eq__': <slot wrapper '__eq__' of 'list' objects>, 
'__ne__': <slot wrapper '__ne__' of 'list' objects>, 
'__gt__': <slot wrapper '__gt__' of 'list' objects>, '__ge__': <slot wrapper '__ge__' of 'list' objects>, '__iter__': <slot wrapper '__iter__' of 'list' objects>, 
'__init__': <slot wrapper '__init__' of 'list' objects>, '__len__': <slot wrapper '__len__' of 'list' objects>,
 '__getitem__': <method '__getitem__' of 'list' objects>, '__setitem__': <slot wrapper '__setitem__' of 'list' objects>,
 '__delitem__': <slot wrapper '__delitem__' of 'list' objects>, '__add__': <slot wrapper '__add__' of 'list' objects>,
 '__mul__': <slot wrapper '__mul__' of 'list' objects>, '__rmul__': <slot wrapper '__rmul__' of 'list' objects>, 
'__contains__': <slot wrapper '__contains__' of 'list' objects>, '__iadd__': <slot wrapper '__iadd__' of 'list' objects>, '__imul__': <slot wrapper '__imul__' of 'list' objects>,
 '__reversed__': <method '__reversed__' of 'list' objects>, '__sizeof__': <method '__sizeof__' of 'list' objects>,
 'clear': <method 'clear' of 'list' objects>, 'copy': <method 'copy' of 'list' objects>, 'append': <method 'append' of 'list' objects>,
 'insert': <method 'insert' of 'list' objects>, 'extend': <method 'extend' of 'list' objects>, 'pop': <method 'pop' of 'list' objects>,
'remove': <method 'remove' of 'list' objects>, 'index': <method 'index' of 'list' objects>, 'count': <method 'count' of 'list' objects>,
 'reverse': <method 'reverse' of 'list' objects>, 'sort': <method 'sort' of 'list' objects>, '__class_getitem__': <method '__class_getitem__' of 'list' objects>, 
'__doc__': 'Built-in mutable sequence.\n\nIf no argument is given, the constructor creates a new empty list.\nThe argument m

Oh, there really isn't one. What's going on here? Then why can we actually get the length of a list using the len () function? Theoretically speaking, lists should have a length attribute. Most objects that len () applies to have a method called __len__() that returns the same value. So len (list) is essentially calling list.__len__()(len () is a built-in function, and __len__ is a magic method).

Then why not use the __len__ method but the len () function?

Obviously, len is a special double-underscore method, which means we should not call it directly. The main reason is efficiency: using the built-in function to call the magic method not only provides additional services, but also the interpreter has been optimized to run faster than calling the method directly. By calling the len () function, we can directly access the ob_size field of the underlying C-language PyVarObject struct of Python variable-length containers, which stores the number of items in the container. len (my_object) directly reads the value of the ob_size field, which is much faster than calling the __len__ method.

Another reason is maintainability. Python developers may modify len () in the future to calculate the length of objects that do not have a len method, for example, by counting the items returned by an iterator. Then they only need to modify one function instead of all the len methods.

Apart from the example of __len__, the most common example is that when you need to get a certain element from a list, you can use the corresponding index to retrieve the value, for instance, list[key]; this is implemented behind the scenes via the __getitem__ method. To obtain the value of my_list[key], the interpreter actually calls my_list.__getitem__(key).

Then how can we obtain these special methods of Python's built-in data types? It's extremely simple — just apply the dir ([object]) function, which can return a list of an object's attributes and methods. Taking the List class as an example, we only need to enter the following statement:

print(dir(list))

Output is as follows:

 ['__add__', '__class__', '__class_getitem__', '__contains__', '__delattr__', 
'__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', 
'__getattribute__', '__getitem__', '__getstate__', '__gt__', '__hash__', 
'__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', 
'__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', 
'__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', 
'__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 
'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']

In addition, if you want to know how to use these methods, you can use the help function:

print(help(list.__le__))

Output is as follows:

0

Actually, the common magic methods in Python can be roughly divided into the following categories: construction and initialization, class representation, operators, access control, container class operations, callable objects, serialization operations, etc. We have summarized the commonly used magic methods in the following figure:

4 6
The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 44 Python Built-in functions and Magic Methods – Overriding Magic Methods of Built-in Types
The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 44 Python Built-in functions and Magic Methods – Overriding Magic Methods of Built-in Types
The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 44 Python Built-in functions and Magic Methods – Overriding Magic Methods of Built-in Types
The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 44 Python Built-in functions and Magic Methods – Overriding Magic Methods of Built-in Types
The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 44 Python Built-in functions and Magic Methods – Overriding Magic Methods of Built-in Types
The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 44 Python Built-in functions and Magic Methods – Overriding Magic Methods of Built-in Types
The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 44 Python Built-in functions and Magic Methods – Overriding Magic Methods of Built-in Types
The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 44 Python Built-in functions and Magic Methods – Overriding Magic Methods of Built-in Types

Override the magic methods of built-in types

Method overriding refers to the process in which a subclass re-implements an accessible method of its superclass, with the return value and formal parameters remaining unchanged. The advantage of overriding is that subclasses can define their own specific behaviors as needed.

Here, we will take the __repr__ and __str__ methods among the magic methods as examples to explain how to override the magic methods of built-in types.

The so-called repr is a special method in Python classes, provided by the object class. Since all classes are subclasses of object, they all inherit this method. It is a "self-description" method, which usually implements the following function: when an instantiated object of a class is directly printed, the system will output the self-description information of the object to inform the outside world of the state information of the object. Normally, when someone directly outputs an instantiated object, the intention is usually to learn the basic information of the object, such as what attributes the object has and what their respective values are.However, since the `repr` method provided by `object` always returns a string in the format of (class name + "object at" + memory address), this value cannot truly fulfill the function of self-description. Therefore, if you want to implement the "self-description" feature in a custom class, you must override the `repr` method.

Similar to repr, there is the __str__ method. Both repr and str are methods used for display: str is user-oriented, while repr is intended for programmers:

Meanwhile, we can also overload arithmetic operators. In the above example of implementing a set with a list, the following code demonstrates the overloading of arithmetic operators:

    # Overload the & operator to compute the intersection
    def __and__(self, other):
        return self.intersect(other)
    # Overload the | operator to compute the union
    def __or__(self, other):
        return self.union(other)
    # When an instance of the class is printed directly, the system outputs the object's self-description
    def __repr__(self):
        return 'Set:' + repr(self.data)

Let's take another interesting example to illustrate operator overloading for arithmetic operations: the following code creates a special type of integer that always returns 0 whenever two instances of this integer are added together:

class SillyInt(int):
    # Override the __add__ method
    def __add__(self, num):
        return 0
    
a = SillyInt(1)
b = SillyInt(2)

print(a+b)

The following is the running result. Although this example has no practical application scenario, it can still well illustrate how to override arithmetic operator methods:

0

The rewritten __add__ method can be added to any class we write ourselves. If I use the + operator on an instance of this class, __add__ will be invoked. This is exactly how the concatenation of strings, tuples, and lists is implemented. All special methods work this way. If you want to use the `x in myobj` syntax for custom objects, you can implement the __contains__ method. If you want to use the `myobj[i] = value` syntax, simply provide the __setitem__ method.If you want to use the syntax `something = myobj[i]`, you need to implement the `__getitem__` method.

347622978