python parameter passing

              I used to be foolish enough to tell the difference between * and ** when passing parameters in Python. As soon as you see * and **, the concept of pointers comes to mind. I finally figured it out today. record it

 

 

# -*- coding:utf-8 -*-

'''
Parameter passing:
package transfer

'''

'''
Package location parameter: what is collected is a tuple
'''
def package_position (* all_arguments ):
print all_arguments
# print the result as (1, 4, 6)
for k in all_arguments:
print k
 

'''
Package keyword parameter: what is collected is a dictionary
'''
def package_keyword (** all_arguments ):
print all_arguments
#Print result is {'a': 1, 'c': 3, 'b': 2}
#print k,v
for k,v in all_arguments.items():
all_arguments[k] = v + 1

'''
Mixed use of positional parameter passing and keyword parameter passing
'''
def packeage_min (* position ,** keywords ):
print position
print keywords

#打印结果
#(1, 2, 3)
#{'a': 7, 'c': 9, 'b': 8}


'''
解包裹
'''
def unpackage( a, b, c):
print (a,b,c)



if __name__ == '__main__':
#package_position(1,4,6)
args2={ 'a': 4, 'b': 5, 'c': 6}
package_keyword(**args2)
#packeage_min(1,2,3,a=7,b=8,c=9)
args = ( 1, 3, 4)
#unpackage(*args) #在args前加上*,来提醒Python,我想把元组拆成三个元素,每个元素对应函数的一个位置参数
args2={ 'a': 4, 'b': 5, 'c': 6}
#unpackage(**args2) #在args2前加上**,让字典的每个键值对作为一个关键字传递给函数




Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326604773&siteId=291194637