Third, the parameters of the function
1. The parameter type of the function
Below we will understand these parameters one by one.
2. Default parameters
E.g:
# -*- coding: UTF-8 -*-
def print_user_info( name , age , sex = '男' ):
# 打印用户信息
print('昵称:{}'.format(name) , end = ' ')
print('年龄:{}'.format(age) , end = ' ')
print('性别:{}'.format(sex))
return;
# 调用 print_user_info 函数
print_user_info( '两点水' , 18 , '女')
print_user_info( '三点水' , 25 )
Output result:
昵称:两点水 年龄:18 性别:女
昵称:三点水 年龄:25 性别:男
# 如果 b 是一个 list ,可以使用 None 作为默认值
def print_info( a , b = None ):
if b is None :
b=[]
return;
b=[]
not ?That is, it is written like this:
def print_info( a , b = [] ):
return;
right?
I didn't find any errors after running it, can I write it like this?
A special note here: the value of the default parameter is an immutable object, such as None, True, False, a number or a string , if you do it like the above, you will encounter when the default value is modified elsewhere to all kinds of trouble.
These changes will affect the default value the next time this function is called.
An example is as follows:
# -*- coding: UTF-8 -*-
def print_info( a , b = [] ):
print(b)
return b ;
result = print_info(1)
result.append('error')
print_info(2)
The result of the output:
[]
['error']
We can do this:
_no_value =object()
def print_info( a , b = _no_value ):
if b is _no_value :
print('b 没有赋值')
return;
Here object
is the base class for all classes in python. You can create instances of the object
class , but these instances are of little practical use, since it doesn't have any useful methods, nor any instance data (because it doesn't have any instance dictionary, and you can't even set any property values). The only thing you can do is test for identity. This feature is also used to determine whether there is a value input.
3. Keyword arguments (positional arguments)
-
Since we don't have to worry about the order of the arguments, using functions is much simpler.
-
Assuming other parameters have default values, we can assign values to only those parameters we want
Specifically look at the example:
# -*- coding: UTF-8 -*-
def print_user_info( name , age , sex = '男' ):
# 打印用户信息
print('昵称:{}'.format(name) , end = ' ')
print('年龄:{}'.format(age) , end = ' ')
print('性别:{}'.format(sex))
return;
# 调用 print_user_info 函数
print_user_info( name = '两点水' ,age = 18 , sex = '女')
print_user_info( name = '两点水' ,sex = '女', age = 18 )
Output value:
昵称:两点水 年龄:18 性别:女
昵称:两点水 年龄:18 性别:女
4. Variable length parameters
*
.E.g:
# -*- coding: UTF-8 -*-
def print_user_info( name , age , sex = '男' , * hobby):
# 打印用户信息
print('昵称:{}'.format(name) , end = ' ')
print('年龄:{}'.format(age) , end = ' ')
print('性别:{}'.format(sex) ,end = ' ' )
print('爱好:{}'.format(hobby))
return;
# 调用 print_user_info 函数
print_user_info( '两点水' ,18 , '女', '打篮球','打羽毛球','跑步')
The result of the output:
昵称:两点水 年龄:18 性别:女 爱好:{'hobby': ('打篮球', '打羽毛球', '跑步')}
*hobby
is a variable parameter, and hobby is actually a tuple (tuple)This method is to add in front of the parameter **
, and change the above example as follows:
# -*- coding: UTF-8 -*-
def print_user_info( name , age , sex = '男' , ** hobby ):
# 打印用户信息
print('昵称:{}'.format(name) , end = ' ')
print('年龄:{}'.format(age) , end = ' ')
print('性别:{}'.format(sex) ,end = ' ' )
print('爱好:{}'.format(hobby))
return;
# 调用 print_user_info 函数
print_user_info( name = '两点水' , age = 18 , sex = '女', hobby = ('打篮球','打羽毛球','跑步'))
The result of the output:
昵称:两点水 年龄:18 性别:女 爱好:{'hobby': ('打篮球', '打羽毛球', '跑步')}
By comparing the above example and this example, we can know that it *hobby
is a variable parameter, and hobby is actually a tuple (tuple), **hobby
a keyword parameter, and hobby is a dict (dictionary)
5. Only accept keyword arguments
This effect can be achieved by placing mandatory keyword arguments after a *
parameter or a single one, for example:*
# -*- coding: UTF-8 -*-
def print_user_info( name , *, age , sex = '男' ):
# 打印用户信息
print('昵称:{}'.format(name) , end = ' ')
print('年龄:{}'.format(age) , end = ' ')
print('性别:{}'.format(sex))
return;
# 调用 print_user_info 函数
print_user_info( name = '两点水' ,age = 18 , sex = '女' )
# 这种写法会报错,因为 age ,sex 这两个参数强制使用关键字参数
#print_user_info( '两点水' , 18 , '女' )
print_user_info('两点水',age='22',sex='男')
As you can see from the example, if you age
do sex
not use keyword arguments, an error will be reported.
In many cases, using mandatory keyword arguments is more explicit and more readable than using positional arguments. Using mandatory keyword arguments is also better than using **kw
parameters and mandatory keyword arguments can also be useful in some more advanced situations.
Featured Article Recommendations
Follow me and catch the latest and most trendy black technology together