first edited October 23, 2017, Monday
Summary
I. Last Class review
Two. Defining functions
Three. Variable parameters
Homework
Summary today
Summary
References: Alex
- Defining functions
- Calling functions
- function return value
- Parameters of the function
I. Last Class review
- Write supplement
- F.write (' 111\n ')
F.write (' 111\n ')
- F.writelines ([' 111\n ', ' 222\n ']) #writelines usage equivalent to write multiple lines
- The Writelines () method is used to write a sequence of strings to a file.
This sequence string can be generated by an iterative object, such as a list of strings.
Line break required to make a newline \ n
- Truncate
- The truncate () method is used to truncate from the beginning of the first line of the file, truncate the file to size characters, no size to truncate from the current position, and all characters after the truncation after the V are deleted, where the line break on the Widnows system represents a 2-character size
Two. Defining functions
- function definition: A function refers to the collection of a set of statements by a name (function name) to encapsulate, to execute this function, just call their function name to
- Characteristics:
Reduce duplicate code
To make the program extensible
Make programs easier to maintain
- Note: The definition of a function is equivalent to the assignment of a variable, the function name equals the variable name, the function's code is equivalent to the value of the variable, and the definition phase detects only the correct syntax of the function, but the call stage is different
- function classification
- Built-in functions
- Custom functions
- def function name (ARG1,ARG2,ARG3):
' Description information '
function body
Return 1
- Definition and invocation of functions
- Define an parameterless function: The statement form for a function call when called
foo()
- Define a parameter function: There is a parameter in the definition, the call must pass the parameter, and generally have the return value; The expression form of the function call when called
res=bar(1,2)
- Defining an empty function
- return value of the function
- Does not specify return in the function, the return value of this function is None
- If return returns a value of multiple, it is returned as a tuple
- Summarize:
- can return any type
- Return 1----> 1
- Return-----> (a)
- No return-----> None
- function execution to return will end
- Decompression of variables
- Parameters of the function
- Formal parameters: In the function definition phase, the parameter defined in parentheses is essentially the variable name
- Arguments: In the function call phase, the parameters defined in parentheses must have a definite value that can be used as the value of the variable
- Note: The value of the argument is immutable type
- Do not modify the global variables in the function
def foo(x,y) #形参 print(x) print(y)foo(1,2) #实参
Three ways of transmitting values at the angle of an argument
Pass Value by Location:
def foo(x,y): print(x) print(y) foo(1,2)
-
Pass Value by keyword
def foo(x,y): print(x) print(y) foo(x=1,y=2)
-
- mixed with
def foo(x,y): print(x) print(y) foo(1,y=2)
Analysis from the angle of formal parameters
Positional parameters, parameters that must be passed to the value
def foo(x,y):print(x)print(y)foo(1,2)
-
- Default parameters
- The default parameter must be placed after the position parameter
def foo(x,y=1): print(x) print(y) foo(2)
-
sex =‘male‘ def register(sex=x): #register-----[sex = ‘male‘] print(sex) sex = None register() #输出为male
-
- Do not modify the global variables in the function (not recommended in the following way)
x=[] def register(name,name_list=x): name_list.append(name) register(‘ASB‘) register(‘YSB‘) register(‘WSB‘) print(x) #[‘ASB‘,‘YSB‘,‘WSB‘]
Three. Variable parameters
- *args
def foo(x,*args): #args = (2,3,4,5,6,‘a‘,‘b‘) print(x) print(args) foo(1,2,3,4,5,6,‘a‘,‘b‘)
-
- Args is mixed with positional parameters and default parameters: *args to be placed after the position parameter
def foo(x,*args,y=2) print(x) print(args) print(y) foo(1,2,3,4,5,y=1000) #>>1;(2,3,4,5);1000
- From the angle of the formal parameter
def foo(*args): #args=(1,2,3) print(args)foo(1,2,3)
- Angle of argument
def bar (x,y,z) print(x) print(y) print(z)bar(*(1,2,3)) #bar(1,2,3)
- **kwargs
def foo(x,**kwargs): print(x) print(kwargs)foo(1,y=2,a=3,b=4) #<<1;{‘b‘:4,‘y‘:2,‘a‘:3}
- Note: **kwargs (by keyword), the rest will be given after the location of the * *, processing the result of the {} dictionary form
* Args (value by location), by keyword, the rest will give *, processing results as () tuple form
- mixed with
def foo(x,*args,**kwargs): print(x) print(args) print(kwargs)foo(1,y=1,z=2) #>>1,(),{‘y‘:1,‘z‘:2}
- From the angle of the formal parameter
def foo(**kwargs): #{‘x‘: 1, ‘y‘: 2, ‘z‘: 3} print(kwargs)foo(x=1,y=2,z=3)
3. Angle of argument
def foo(x,y,z=3): print(x) print(y) print(z)foo(**{‘x‘:1,‘y‘:2,‘z‘:3}) #foo(x=1,y=2,z=3)
- Nesting
def auth(name,password,sex=‘male‘): print(name) print(password) print(sex)def foo(*args,**kwargs): #args =(‘egon‘,‘123‘) kwargs= {} print(‘from foo‘) auth(*args,**kwargs) #auth(*(‘egon‘,‘123‘),**{})---------auth(‘egon‘,‘123‘)foo(‘egon‘,‘123‘)
Homework
- No
Summary today
- Pending additions
Python Full Stack road DAY18