function definition
- The function code block begins with a def keyword followed by the function identifier name and parentheses ().
- Any incoming parameters and arguments must be placed in the middle of the parentheses. Parentheses can be used to define parameters.
- The first line of the function statement can optionally use the document string-for storing the function description.
- The function contents begin with a colon and are indented.
- return [expression] ends the function, optionally returning a value to the caller. Return without an expression is equivalent to returning None.
Grammar
def functionname( parameters ): "函数_文档字符串" function_suite return [expression]
Can change (mutable) and non-changing (immutable) objects
In Python, ==strings, tuples, and numbers== are objects that cannot be changed, and ==list,dict== are objects that can be modified.
- Immutable Types :
Variable assignment a=5 and then assign value a=10, here is actually reborn into an int value object 10, and then let a point to it, and 5 is discarded, not change the value of a, the equivalent of a new student into a.
- Variable type :
Variable assignment la=[1,2,3,4] After assigning a value la[2]=5 is to change the value of the third element of List LA, itself LA is not moving, but its internal part of the value has been modified.
Global variables and local variables
A variable that is defined inside a function has a local scope
Defines an owning global scope outside the function.
If you want to declare a variable inside a function as an external global scope, use the global
declaration
Pass the reference
#可写函数说明def printme( str ): "打印任何传入的字符串" print str; return;prinme(‘My string‘)
#可写函数说明def printme( str ): "打印任何传入的字符串" print str; return;#调用printme函数printme( str = "My string");
#可写函数说明def printinfo( name, age = 35 ): "打印任何传入的字符串" print "Name: ", name; print "Age ", age; return;#调用printinfo函数printinfo( age=50, name="miki" );printinfo( name="miki" );
- Indefinite length parameter
When you pass in the list, add an asterisk
When you pass in a dictionary, add two asterisks
# 可写函数说明def printinfo( arg1, *vartuple ): "打印任何传入的参数" print "输出: " print arg1 for var in vartuple: print var return;# 调用printinfo 函数printinfo( 10 );printinfo( 70, 60, 50 );
Anonymous parameter lambda
- Lambda is just an expression, and the function body is much simpler than def.
- The body of a lambda is an expression, not a block of code. Only a finite amount of logic can be encapsulated in a lambda expression.
The lambda function has its own namespace and cannot access parameters outside its own argument list or in the global namespace.
- Although the lambda function appears to be only a single line, it is not equivalent to a C or C + + inline function, which is designed to call small functions without consuming stack memory to increase operational efficiency.
语法lambda [arg1 [,arg2,.....argn]]:expression# 可写函数说明sum = lambda arg1, arg2: arg1 + arg2;# 调用sum函数print "相加后的值为 : ", sum( 10, 20 )
Python function Detailed Summary