Python in depth 01 special methods and multi-paradigm

Source: Internet
Author: User

Python is all object , but at the same time, Python is a multi-paradigm language (MULTI-PARADIGM), you can not only use the object-oriented way to write programs, but also in a process-oriented way to write the same function of the program (there are functional, declarative, etc., We are not in depth yet). Python's multi-paradigm relies on special methods in Python objects (special method).

The Special method name has two underscores before and after each . Special methods are also used as magic methods, which define many of the Python syntax and expressions , as we will see in the following example. Python also has "special privileges" when it defines special methods in the object. For example, a class that defines the __init__ () method automatically executes the action in the __init__ () method when the object is created.

(You can use DIR () to see special methods owned by an object, such as dir (1))

operator

The Python operator is implemented by invoking the object's special method. Like what:

' ABC ' ' XYZ ' # connection string

The following actions are actually performed:

' ABC '. __add__ ('xyz')

So, in Python, whether two objects can be additive, the first thing to see is whether the corresponding object has a __add__ () method. Once the corresponding object has the __add__ () method, even if the object is mathematically non-additive, we can express the operation defined by OBJ.__ADD__ () in the form of addition. In Python, operators play a function of simplifying writing , but it relies on special methods.

Python does not force users to use object-oriented programming methods. Users can choose how they prefer to use them (such as choosing to use the + symbol or using the more object-oriented __add__ () method). A special method is always a little more cumbersome to write.

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

(1.8). __MUL__ (2.0)

TRUE.__OR__ (False)

built-in functions

Like operators, many built-in functions are special methods of invoking objects. Like what

Len ([+]) # Returns the total number of elements in the table

Actually doing is

[A.] __len__ ()

Relative to __len__ (), the built-in function Len () also plays a role in simplifying writing .

Try the following, think about its corresponding built-in function

( -1). __abs__ ()

(2.3). __int__ ()

table (list) element reference

Here are our common TABLE element reference methods

Li = [1, 2, 3, 4, 5, 6]print(li[3])

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

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

Try to look at the following operation, think of its corresponding

li.__setitem__ (3, 0)

{' A ': 1, ' B ': 2}.__delitem__ (' a ')

function

As we've already said, in Python, a function is also an object. In fact, any object with a __call__ () special method is considered to be a function. For example, the following:

class Samplemore (object):
def __call__ (Self, a): return A + 5
= Samplemore () # A function Objectprint(Add (2)) # Call function Map (add, [2, 4, 5]) # Pass around function object

Python in depth 01 special methods and multi-paradigm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.