大蟒日志5

十、函数2

  1. 默认参数值
  2. 关键参数值
  3. 可变参数VarArgs
# 2018.3.31
# 1.Default Argument Value
# Note that the default argument value should be a constant.
def say(message, times=1):
    print(message * times)
say('Hello ')
say('Kitty ', 5)
say(-10)
say(5, 3)
# 注意: 参数列表从后向前default, 不可从前往后

# 2.Keyword Argument
def func(a, b=5, c=10):
    print('a is', a, '\nb is', b, '\nc is', c)

func(21,-3.3)
func(35,c=24)
func(c=1, a=2)
'''
输出结果:
a is 21 
b is -3.3 
c is 10
a is 35 
b is 5 
c is 24
a is 2 
b is 5 
c is 1
'''
# 按参数列表中的顺序依次取值
# 若有默认参数值设置, 除默认参数值外, 其他仍然按列表顺序


# 2018.4.1
# 3.VarArgs parameter (VarArgs = Variable number of Arguments)
def total(a=5, *numbers, **phonebook):
    print('a', a)

    for single_item in numbers:
        print('single_item', single_item)

    for first_part, second_part in phonebook.items():
        print(first_part, second_part)

print(total(10,1,2,3, Jack=1123, John=2231, Inge=1560))
'''
显示结果:
a 10
single_item 1
single_item 2
single_item 3
Jack 1123
John 2231
Inge 1560
None
①All the positional arguments from that point till the 
end are collected as a tuple(数组) called 'param' 
②**param all the keyword argument from that point till 
the end are collected as a dictionary called 'param'
③tuple and dictionary, learn them in later chapters
'''
#牵扯到元组和字典,一时理解得不够透彻,后续更新……

【声明】本博文为学习笔记,含有个人修改成分,并非完全依照《a byte of python》,仅供参考。若发现有错误的地方,请不吝赐教,谢谢。同时,我也会不定期回顾,及时纠正。#

猜你喜欢

转载自blog.csdn.net/csdner_0/article/details/79773636