Special methods and multi-paradigm for Python deep learning

Source: Internet
Author: User
This article mainly introduces the special methods and multi-paradigm for Python deep learning. special methods are like magic methods in PHP, multi-paradigm refers to the ability to write programs using object-oriented, process-oriented, functional, declarative, and other methods. if you need a friend, you can refer to all Python objects, python is also a multi-paradigm language. you can not only use object-oriented programming, 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:

The code is as follows:


'ABC' + 'XYZ' # connection string


The following operations are performed:

The 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.

The 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

The code is as follows:


Len ([, 3]) # returns the total number of elements in the table


What we actually do is

The 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.

The code is as follows:


(-1). _ abs __()

(2.3). _ int __()


Table (list) element reference

The following are common table element reference methods:

The 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.

The 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

The 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:

The 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.

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.