Python Grammar notes (i)

Source: Internet
Author: User

1. Multiple functions or class definitions in Python can be placed in a. py file as a module. In the. py file of the module, it is generally written if __name__ = = ' __mian__ ' is used to execute some functions in the module separately.

2. Python multiple modules are placed in the same folder and a new __init__.py file is created in the folder, the folder can form a package. The __init__.py file tells the Python interpreter that the folder is a package, and the contents of the __init__.py can be empty.

3. In a python file you can import a package, a module in a package, a module, a class or function in a module, for example;

Import Pack1  #这样在使用pack1中的模块或者其他东西, you need to specify the module name before Pack1import pack1.module1# when using Module1, need to write Pack1.module1.xxximport Module1 from pack1# this way, in the use of Module1, you can not write Pack1, only write Module1.xxximport module1.classa# when using ClassA, you need to specify Module1.classaimport ClassA from module1# when using ClassA, only need Classaimport module1.func1# when using func1, need to write Module1.func1import func1 from module1# when using func1, just write func1### #注意, the above is the default namespace does not conflict, that is, this situation does not occur: # Both Module1 and Module2 have ClassA, and the import ClassA from Module1, the import ClassA from #module2 because of this conflict

4. When the Python program executes, the search path for the package and module is:

(1) Current folder

(2) The path defined by the system variable pythonpahth

(3) Installation path of Python standard library

5. The Python function can pass parameters in the following ways:

(1) Position transfer parameters

That is, the order of the arguments passed in is consistent with the sequence of function definitions, and if there are default parameters, the default parameters are placed in the final position.

(2) keyword pass-through parameters

That is, when the function is called, the func (arg1 = real_arg1, arg2 = REAL_ARG2) is indicated. ), where the formal parameters and arguments are explicitly corresponding, the order of the actual arguments does not have to be written in the order of the parameters when the function is defined.

Positional pass parameters and keyword pass parameters can be mixed, but positional pass parameters need to be placed before keyword-passed arguments, such as Func (real_arg1, arg2 = Real_args, Arg3 = Real_arg3 ...).

(3) Parcel delivery

There are two kinds of transitive methods, such as the non-definite parameters of C language.

(1) passed through a tuple, the function is defined as:

def func (*args):

Xxxx

When actually called:

Func (1,2,3,4) #1, 2,3,4 is automatically packaged into a single tuple

(2) passed through a dictionary, the function is defined as:

def func (**args):

Xxxx

When actually called:

Func (a= ' Hello ', B = ' world ', C = 123) #参数自动封装成了一个字典 {' A ': ' Hello ', ' B ': ' World ', ' C ': 123}

(4) Solution package delivery

When a function is defined with more than one argument, and we want to separate the elements of a tuple or a dictionary as multiple parameters of a function, when a tuple or dictionary is passed as a parameter to a function, you can:

def func (A, B, c):

Xxx

When called:

t = (--)

Func (t)

T2 = {' A ': 1, ' B ': All, ' C ': ' Hello World '}

Func (T2) #将字典的键值对分别作为各个参数

When defining or invoking parameters, several methods of passing parameters can be mixed. But be careful about the sequence in the process. The basic principle is that the first position, then the keyword, then wrap the position, and then wrap the keyword, and according to the above-mentioned principle and carefully distinguish.

Note: Be aware of the distinction between when defined and when it is called. Wrapping and wrapping is not the opposite, it is a two relatively independent process.

Python Grammar notes (i)

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.