函数参数传递

'''
1 位置参数不能在关键字参数之后
2 *args只能用于位置参数  把位置参数打包成元组
3 **kwargs只能用于关键字参数 把关键字参数打包成字典
'''

import time

def logger():
    time_format = "%Y-%m-%d %X"
    current_timestamp = time.strftime(time_format)
    with open("log.txt", 'a', encoding="UTF-8") as f:
        f.write("%s end edition\n" % current_timestamp)
        
def test1():
    print("is in test1")
    logger()
def test2():
    print("is in test2")
    logger()
#函数没有写返回值,返回None,多个返回值,打包成一个元组返回
def test3():
    print("is in test3")
    logger()
    return 1, [2, 3], {"name":"xiaobai", "age":26}
def test4(x, y, z):
    print(x)
    print(y)
    print(z)
# *args把所有位置参数打包成一个元组
def test5(x, *args):
    print(x)
    print(args)
# **kwargs把所有关键字参数打包成一个字典
def test6(x, **kwargs):
    print(x)
    print(kwargs)
def test7(x, name="laoli", *args, **kwargs):
    print(x)
    print(name)
    print(args[2])
    print(kwargs['hobby'])
test1()
test2()
a = test3()
print(a)
test4(1, 2, 3)
test4(x=2, y=1, z=3)
test4(3, z=4, y=5)
#位置参数不能在关键词参数后面
# test4(x=5, 6, z=7)
test4(3, y=4, z=5)
test5(6, "xiaobai", "xiaohei", "xiaowang")
test6("xiaohong", age=26, height=170)
print("------------")
test7(1, "laohei", 2, 3, 4, 5, hobby="car", weight=140)

  

posted @ 2018-03-23 16:54  与君同悦  阅读(96)  评论(0)    收藏  举报