函数/方法

 1 # def 方法名(形参):
 2 #   方法体
 3 #   return value
 4 def add(a, b):
 5     print('a=', a, type(a))
 6     print('b=', b, type(b))
 7     return None;
 8 
 9 
10 # 元组的元素,元组
11 def dyn_tuple(*args):
12     print(args, type(args))
13     return args
14 
15 
16 # 字典
17 def dyn_dict(**kwargs):
18     print(kwargs, type(kwargs))
19     return kwargs
20 
21 
22 def anyParameter(*args, **kwargs):
23     print(args, type(args), kwargs, type(kwargs))
24     return None
25 
26 
27 # 调用方法: 方法名(实参)
28 
29 # 普通参数:根据顺序传入参数     a= 1 <class 'int'>     b= (1, 3) <class 'tuple'>
30 add(1, (1, 3))
31 
32 # 指定参数:参数可以顺序任意:a= {'k2': 'v2', 'k1': 'v1'} <class 'dict'>  b= {1, 55, 33} <class 'set'>
33 add(b={1, 33, 55}, a={'k1': 'v1', 'k2': 'v2'})
34 
35 # 默认参数:默认参数必须放在最后面,否则会报错 a= ['mark', 'jan', 'feb'] <class 'list'>   b= 10 <class 'int'>
36 add(['mark', 'jan', 'feb'], b=10)
37 # 动态参数1  元组的元素 ([12, 32, 4],) <class 'tuple'>
38 dyn_tuple([12, 32, 4, 5])
39 
40 # 动态参数2 元组 (12, 32, 4, 5) <class 'tuple'>
41 dyn_tuple(*[12, 32, 4, 5])
42 
43 # 动态参数3 字典    {'k1': 123, 'k2': '21'} <class 'dict'>
44 dyn_dict(k1=123, k2='21')
45 
46 # (123,) <class 'tuple'> {} <class 'dict'>
47 anyParameter(123, 'jvoe')
48 
49 # (1, 2, 4, '4') <class 'tuple'> {} <class 'dict'>
50 anyParameter(*{1, 2, 4, '4'})
51 
52 #(1, 2, 4, '4') <class 'tuple'> {'k1': 1, 'k2': 'v2'} <class 'dict'>
53 anyParameter(*{1, 2, 4, '4'},k1=1,k2='v2')

 

posted on 2018-01-15 18:44  jovelove  阅读(79)  评论(0编辑  收藏  举报