Python Note 6: Modules

Source: Internet
Author: User

6. Module (A. py file is called a modular module)

Import statement
Functions/variables like _xxx and __xxx are nonpublic (private) and should not be directly referenced
function definition: 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.
Instance properties and Class properties
Never use the same name for instance properties and class properties, because instance properties of the same name will mask class properties, but when you delete an instance property and then use the same name, the class attribute is accessed.

@property adorners -Simplify your code, avoid writing duplicate code for each function Python decoration mode-can decorate a function, method, or class
* * Case 1 (decorator with no parameters, two-tiered nesting):

ImportFunctools#Import Functools Packagedeflog (func): @functools. Wraps (func)#wrapper.__name__ = func.__name__defWrapper (*args, **kw):#wrapper can receive arbitrary parameters of the callPrint 'Call %s ():'% func.__name__ #print a row of logs before running the Func () functionreturnFunc (*args, * *kw)returnWrapper@log#equivalent to execute now = log (now)defNow ():#calling Now () will execute the wrapper () function returned in the log () functionPrint '2013-12-25

* * Case 2 (decorator with parameters, three levels of nesting):

ImportFunctoolsdefLog (text):#First Level nestingdefDecorator (func):#Second Level nesting@functools. Wraps (func)defWrapper (*args, **kw):#Third Layer NestingPrint '%s%s ():'% (text, func.)__name__)#print a row of log with parameters before running the func () functionreturnFunc (*args, * *kw)returnwrapperreturnDecorator@log ('Execute')#equivalent to execute now = log (' Execute ') (now)defNow ():Print '2013-12-25'

partial function: Combined with default parameter understanding
--Set default values for some parameters and return a new function, which is easier to invoke after calling this new function.
Case comparison:

def # Traditional Methods return Int (x, Base)
Import # using the partial function method # Binary Conversions

__future__
Case: Using the Python 3.x division directly in Python 2.7 's code, can be achieved through the division of the __FUTURE__ module
From __future__ Import Division

Python Note 6: Modules

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.