Liaoche Python excerpt 6

Source: Internet
Author: User
Tags wrapper

1 defNow ():2     Print('Hello')3 4 ImportFunctools5 6 deflog (func):7 @functools. Wraps (func)8     defWrapper (*args,**kw):9         Print('begin call%s ():'% func.__name__)Ten         func_tmp=func (*args,**kw)  One         Print('End Call%s ():'% func.__name__) A         return  func_tmp  -     returnWrapper

2, a simple summary functools.partial of the role of a function is to fix some parameters (that is, set the default value), return a new function, call this new function is more simple.

3.

What are the benefits of using modules?

The greatest benefit is that the maintainability of the code is greatly improved. Second, writing code does not have to start from scratch. When a module is written, it can be referenced elsewhere. When we write programs, we often refer to other modules, including Python built-in modules and modules from third parties.

The use of modules also avoids conflicting function names and variable names. Functions and variables of the same name can exist in different modules individually, so we do not have to think about names that conflict with other modules when we write our own modules. But also be aware that try not to conflict with the built-in function name. Click here to see all of Python's built-in functions.

You may also think, what if different people write the same module name? To avoid module name collisions, Python introduces a way to organize modules by directory, called packages.

4, the usual function and variable name is public, can be directly referenced, such as: abc , x123 , PI etc.;

__xxx__Such variables are special variables, can be directly referenced, but there are special purposes, such as the above __author__ , __name__ is a special variable, hello the module definition of the document can also be accessed with special variables __doc__ , our own variables are generally not used this variable name;

Similar _xxx and __xxx such functions or variables are non-public (private), should not be directly referenced, such as _abc , __abc etc.;

5.

We expose the function in the module and greeting() hide the internal logic with the private function, so that the calling greeting() function does not care about the internal private function details, which is also a very useful way to encapsulate and abstract the code, namely:

Functions that do not need to be referenced externally are all defined as private, and only functions that need to be referenced externally are defined as public.

6. Object-oriented concepts are classes (class) and instances (Instance), and it is important to keep in mind that classes are abstract templates, such as student classes, and instances are specific "objects" that are created from classes, each with the same method, but the data may be different.

7, followed by the class class name, that is Student , the class name is usually the beginning of the word, followed by (object) , indicating the class is inherited from the class, the concept of inheritance we will say later, usually, if there is no suitable inheritance class, the class object is used, this is all classes will eventually inherit the class. You are free to bind attributes to an instance variable, for example, to bart bind an instance to an name attribute.

8, because the class can play the role of the template, so you can create an instance, some of the attributes we think must be bound to fill in. By defining a special __init__ method, when you create an instance, you bind the name property, and score so on.

9, because the class can play the role of the template, so you can create an instance, some of the attributes we think must be bound to fill in. By defining a special __init__ method, when you create an instance, you bind the name property, and score so on.

10, if you want to let the internal properties are not external access, you can put the name of the property with two underscores __ , in Python, the instance of the variable name if the __ beginning, it becomes a private variable (private), only the internal can be accessed, external inaccessible.

11. What are the benefits of inheritance? The biggest benefit is that the subclass obtains the full functionality of the parent class. Because the Animial method is implemented, run() Dog and Cat as its subclass, nothing is done, it automatically has the run() method:

12, when both the subclass and the parent class have the same run() method, we say that the subclass overrides the parent class, and the subclass is run() run() always called when the code is running run() .   In this way, we gain another benefit of inheritance: polymorphism. Add a Animal subclass that does not have to run_twice() make any changes, in fact, any function or method that relies on as a Animal parameter can work without modification, because of polymorphism.

13. If you want to get all the properties and methods of an object, you can use a dir() function that returns a list containing a string

14.

Liaoche Python excerpt 6

Related Article

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.