Everything in Python is an object, but Python is also a multi-paradigm language, and you can write programs that do the same thing (functional, declarative, etc., we won’t go into that yet) in a procedural way, as well as in an object-oriented way. Python’s multiple paradigms rely on special methods in Python objects.

Special method names are preceded by two underscores. Special methods, also known as magic methods, define many Python syntax and expressions, as we’ll see in the examples below. Python also gives “special treatment” to objects when they have special methods defined. For example, a class that defines an __init__() method automatically performs the operations in the __init__() method when an object is created.

(You can use dir() to see what special methods an object has, such as dir(1).)

The operator

Python operators are implemented by calling special methods on objects. Such as:

'ABC' + 'xyz' # concatenate stringCopy the code

The following operations are performed:

'abc'.__add__('xyz')
Copy the code

So, in Python, whether two objects can be added depends first on whether the corresponding object has an __add__() method. Once the corresponding object has an __add__() method, we can express the operations defined by obj.add() in addition form, even if the object is mathematically unadditive. In Python, operators serve to simplify writing, but they rely on special methods.

Python does not force users to use object-oriented programming methods. Users can choose how they like to use it (such as the + symbol or the more object-oriented __add__() method). Special methods are always a little more difficult to write.

Try the following operation to see the effect and think about its corresponding operator

(1.8). The mul (2.0)

True.or(False)

Built-in function

Like operators, many built-in functions are special methods that call objects. Such as

Len ([1,2,3]) # return the total number of elements in the tableCopy the code

What we’re actually doing is

[1, 2, 3]. __len__ ()Copy the code

In contrast to __len__(), the built-in len() function also helps simplify writing.

Try the following and think of its built-in counterpart

(-1).abs()

(2.3). The int ()

Table element references

Here are some common table element references

li = [1, 2, 3, 4, 5, 6]
print(li[3])
Copy the code

When the above program runs to Li [3], Python finds and understands the [] symbol and then calls the __getitem__() method.

li = [1, 2, 3, 4, 5, 6]
print(li.__getitem__(3))
Copy the code

Try looking at the following operation and think about the corresponding

li.setitem(3, 0)

{‘a’:1, ‘b’:2}.delitem(‘a’)

function

We’ve already said that in Python, a function is also an object. In fact, any object that has a __call__() special method is treated as a function. Take the following example:

class SampleMore(object):
    def __call__(self, a):
        return a + 5

add = SampleMore()     # A function object
print(add(2))          # Call function    
map(add, [2, 4, 5])    # Pass around function object
Copy the code

Add is an object of the SampleMore class. When called, add performs the addition of 5. Add can also be passed as a function object to the map() function.

Of course, we can use a more “elegant” way, think about what.

conclusion

For built-in objects (such as integers, tables, strings, etc.), special methods are already available in Python. Special methods can be added to implement custom syntax for user-defined objects. Special methods are closer to the roots of Python, and many Python functions rely on special methods. We’ll see more examples in the future.