1.函数修饰符(@)
@与其说是修饰不如说其是引用
def use(f):
def test():
print('this is use test')
f()
return test
@use
def usetest():
print('Hello')
usetest()
# 输出:
# this is use test
# Hello
类似于如下应用:
def use(f):
print('this is use test')
f()
def usetest():
print('Hello')
use(usetest)
2.参数修饰符(*)
参数带一个星号表示参数以元组(tuple)的形式导入:
def use(*arg):
print(arg)
use(1,'2',[2,'3'])
# 输出:
# (1, '2', [2, '3'])
# use(a=1, 2) TypeError: use() got an unexpected keyword argument 'a'
3.参数修饰符(**)?
双星号(**)将参数以字典的形式导入:
def use(**arg):
print(arg)
use(a=1, b='2', c=[2, '3'])
# 输出:
# {'a': 1, 'b': '2', 'c': [2, '3']}
def use(*arg1, **arg2):
print(arg1)
print(arg2)
use(1, b='2', c=[2, '3'])
# 输出:
# (1,)
# {'b': '2', 'c': [2, '3']}
|