Special methods and multi-paradigm for Python deep learning, and python deep learning paradigm

Source: Internet
Author: User
Tags class operator

Special methods and multi-paradigm for Python deep learning, and python deep learning paradigm

Python is an object. But at the same time, Python is also a multi-paradigm language. You can not only write programs in an object-oriented way, you can also use process-oriented methods to compile programs with the same functions (as well as functional and declarative programs, which we will not go into for the time being ). The multi-paradigm of Python depends on the special method (special method) in the Python object ).

The special method names have two underscores. The special method becomes a magic method and defines many Python syntaxes and expressions, as we will see in the following example. When special methods are defined in an object, Python also gives them special privileges ". For example, if the _ init _ () method class is defined, the operation in the _ init _ () method is automatically executed when an object is created.

(You can use dir () to view the special methods of an object, such as dir (1 ))

Operator

Python operators are implemented by calling special methods of objects. For example:
Copy codeThe Code is as follows:
'Abc' + 'xyz' # connection string

The following operations are performed:
Copy codeThe Code is as follows:
'Abc'. _ add _ ('xyz ')

Therefore, in Python, whether two objects can perform addition operations depends on whether the corresponding object has the _ add _ () method. Once the corresponding object has the _ add _ () method, even if this object cannot be added in mathematics, we can use addition to express obj. the operation defined by _ add. In Python, operators simplify writing, but they rely on special methods.

Python does not force users to use object-oriented programming methods. You can select your preferred use method (for example, use the + symbol or use the more object-oriented _ add _ () method ). Writing a special method always requires more trouble.

Try the following operations to see the effect, and then think about its corresponding operators.
Copy codeThe Code is as follows:
(1.8). _ mul _ (2.0)

True. _ or _ (False)

Built-in functions

Similar to operators, many built-in functions are special methods for calling objects. For example
Copy codeThe Code is as follows:
Len ([, 3]) # returns the total number of elements in the table

What we actually do is
Copy codeThe Code is as follows:
[1, 2, 3]. _ len __()

Compared with _ len _ (), the built-in function len () also plays a role in simplifying writing.

Try the following operations and think about its built-in functions.
Copy codeThe Code is as follows:
(-1). _ abs __()

(2.3). _ int __()

Table (list) element reference

The following are common table element reference methods:
Copy codeThe Code is as follows:
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.
Copy codeThe Code is as follows:
Li = [1, 2, 3, 4, 5, 6]
Print (li. _ getitem _ (3 ))

Try to see the following operations and think about the corresponding
Copy codeThe Code is as follows:
Li. _ setitem _ (3, 0)

{'A': 1, 'B': 2}. _ delitem _ ('A ')

Function

As we have already said, in Python, functions are also an object. In fact, any object with a special _ call _ () method is treated as a function. For example:

Copy codeThe Code is as follows:
Class SampleMore (object ):
Def _ call _ (self, ):
Return a + 5

Add = SampleMore () # A function object
Print (add (2) # Call function
Map (add, [2, 4, 5]) # Pass around function object

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

Of course, we can also use a more "beautiful" approach and think about what it is.

Summary

For built-in objects (such as integers, tables, strings, etc.), all the special methods required by them are ready in Python. You can add special methods to customize the syntax of custom objects. Special methods are closer to the underlying Python layer. Many Python functions depend on special methods. We will see more examples later.

Many Python syntaxes are encapsulated based on its object-oriented model. The object model is the skeleton of Python and is a highly functional and powerful Bumblebee. However, Python also provides more concise syntax, allowing you to use different programming forms, so as to hide some object-oriented interfaces when necessary. As we can see, the Camaro sports car collects its mighty gunpowder library to provide doors and seats for human use.


How does one use the special python method customization class _ radd _ and _ iadd?

_ Radd _ is a user-defined class operator and runs "add right ". When the python interpreter runs a statement such as a + B, first find whether the _ add _ operator in a. If a is not defined, search for and execute _ radd _ in B __.
The following is a simple example.
Class:
Pass
Class B:
Def _ radd _ (self, ):
Print 'B. _ radd __'
Return a. v + self. v
Class C:
Def _ add _ (self, B ):
Print 'C. _ add __'
Return self. v + B. v
A = ()
A. v = 1
B = B ()
B .v = 2
C = C ()
C. v = 3
Print a + B # Because a does not contain _ add __, B. _ radd _ is called __
Print c + B # c contains _ add __, so C. _ add _ is called __

As for _ iadd _ (), it is a member function of operator class, which is another call form of the accumulate operator.
A = operator. _ iadd _ (a, B) is equivalent to a + = B

How to use the special _ nonzero _ () method of python? Which will be called. For example,

The _ nonzero _ method of a class is used to convert a class to a Boolean value. It is usually called when the class is used to judge and convert the class into a Boolean value. For example, if A: print 'foo' in the statement will call A. _ nonzero _ () to judge. The following program should help you understand the role of _ nonzero.

Class:
Def _ nonzero _ (self ):
Print 'a. _ nonzero __()'
Return True

Print 'a is not 0' if A () else 'a is 0'
Print bool (())

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.