A Byte of Python - function contact notes

1, the real parameter participating in the definition of the function given the name referred to as "parameter", when you are then provided to call the function value of the function referred to as "arguments"

2, the scope of local variables block they all variables are defined, starting from the definition of their names defined points.

3, global statement if you want to assign a top-level program variable (that is to say it does not exist in any scope, regardless of function or class), then you must tell the Python variable is not local, but global (Global) is. We need to get this done by the global statement.

 

4, the default parameter values ​​for some functions, you may want to make some parameters optional and use default values, you do not want to avoid the situation provides a value to them. The default parameter values ​​can be effective in helping to resolve this situation. You can attach an assignment operator (=) in the function definition to specify a default parameter value for a parameter.

 

 

5, keyword arguments if you have some functions with many parameters and you want only some of which are specified, you can name them to give these parameter assignment - this is the key parameter (KeywordArguments) - we use the name (keyword) instead of position (the way we have been used) to specify the parameters function.

 

 

6, variable parameters Sometimes you may want to define a function which can have any number of variables, that is, the number of parameters is variable, which can be achieved by using an asterisk.

 

 

7, return statement

return statement is used to return from the function, which is the interrupt function. We can also choose to return a value from a function in the interrupt function.

 

 8、DocString

Python has a very beautiful feature called documentation strings (Documentation Strings), usually using another shorter name docstrings when to call it. DocStrings is an important tool that you should use, it can help you better record the program and make it more easily understood.

 

Guess you like

Origin www.cnblogs.com/yimadangxian666/p/11454516.html