About bloggers : Former Internet manufacturer tencent staff , Network security giant Venustech staff , Alibaba cloud development community expert blogger , WeChat official account java Quality creators of basic notes ,csdn High quality creative bloggers , Entrepreneur , Knowledge sharers , Welcome to your attention , give the thumbs-up , Collection .
In the actual development process , You will often encounter many identical or very similar operations , At this time , Code that implements similar operations can be encapsulated as functions , Then call the function where you need it . This can not only realize code reuse , It can also make the code more organized , Increase code reliability . Now let's introduce python Function position parameters of .
When you define a function , You can set default values for function parameters , This parameter is called the default value parameter . Functions with default parameters .
When calling a function with a default value parameter , You do not need to transfer values for formal parameters with default values , At this point, the function will directly use the default value set during function definition , You can also replace its default value by explicit assignment . The definition syntax is as follows :
def Function name (…, The name of the parameter = The default value is ):
The body of the function
example : Default parameter .
def printinfo( name, age = 35 ): # Defined function , Print any incoming strings
print (" name : ", name)
print (" Age : ", age)
return
# call printinfo function
print(printinfo.__defaults__) # Output function default value parameter
printinfo("root" ,50) # Explicit assignment
print ("------------------------")
printinfo("root" ) # Use the default value parameter
give the result as follows .
have access to “ Function name .defaults” View the current values of all default value parameters of the function at any time , Its return value is a tuple , The elements in turn represent the current value of each default value parameter .
def printinfo( name, age = 35 ): # Defined function , Print any incoming strings
print (" name : ", name)
print (" Age : ", age)
return
print(printinfo.__defaults__)
When defining a function with a default value parameter , The default value parameter must appear at the right end of the function parameter list , Otherwise, a syntax error will be prompted .
for example : The definition of the following function is wrong :
def f(a = 1, b, c = 3):
print(a, b, c)
give the result as follows .
When a function is called multiple times without passing a value for a default value parameter , The default value parameter is interpreted and initialized only once during definition , For a list of 、 A dictionary is a variable type of default value parameter , This may lead to logical errors . therefore , Generally speaking , To avoid using lists 、 Dictionaries 、 Set or other variable data type as the default value of function parameters .
example : Read the following procedure , Analyze the running results .
def demo(newitem,old_list = []):
old_list.append(newitem)
return old_list
print(demo('5',[1,2,3,4]))
print(demo('a'))
print(demo('b'))
give the result as follows .
If the default value of a parameter when defining a function is a variable , Then the default value of the parameter only depends on the value of the variable when the function is defined . example : Run the following program , Analyze the running results .
a = 1
def f(n = a):
print(n)
a = 5
f()
give the result as follows .
1、 Liao Xuefeng's official website
2、python Official website
3、Python Programming case tutorial
The above is about Python Function position parameters , You can refer to it , If you think it's good , Welcome to thumb up 、 Collection 、 Looking at , Welcome to wechat search java Basic notes , Relevant knowledge will be continuously updated later , Make progress together .