• day03_03 函数


    func_test1.py

    
    __author__ = "Alex Li"
    #函数
    def func1():
    """testing1"""
    print('in the func1')
    return 0
    #过程
    def func2():
    '''testing2'''
    print('in the func2')
    
    x=func1()
    y=func2()
    
    print('from func1 return is %s' %x)
    print('from func2 return is %s' %y)
    
    

    func_test2.py

    
    __author__ = "Alex Li"
    import time
    
    def logger():
    time_format = '%Y-%m-%d %X'
    time_current = time.strftime(time_format)
    with open('a.txt','a+') as f:
    f.write('%s end action\n' %time_current)
    
    def test1():
    print('in the test1')
    logger()
    
    def test2():
    print('in the test2')
    logger()
    
    def test3():
    print('in the test3')
    logger()
    
    test1()
    test2()
    test3()
    
    

    func_test3.py

    
    __author__ = "Alex Li"
    def test1():
    pass
    
    def test2():
    return 0
    
    def test3():
    return 0,'hello',['a','b','c'],{'name':'alex'}
    
    x=test1()
    y=test2()
    z=test3()
    
    

    func_test4.py

    
    __author__ = "Alex Li"
    def test1():
    print('in the test1')
    
    def test2():
    print('in the test2')
    return 0
    def test3():
    print('in the test3')
    return 1,'hello',['alex','wupeiqi'],{'name':'alex'}
    #return test2
    
    
    x=test1()
    y=test2()
    z=test3()
    print(x)
    print(y)
    print(z)
    
    

    func_test5.py

    
    __author__ = "Alex Li"
    
    def test(x,y,z):
    print(x)
    print(y)
    print(z)
    
    # test(y=2,x=1) #与形参顺序无关
    # test(1,2) #与形参一一对应
    #test(x=2,3)
    test(3,z=2,y=6)
    
    

    func_test6.py

    
    __author__ = "Alex Li"
    
    def conn(host,port=3306):
    pass
    
    def test(x,y=2):
    print(x)
    print(y)
    
    test(1,3)
    
    '''
    #默认参数特点:调用函数的时候,默认参数非必须传递
    #用途:
    1.默认安装
    2.默认设置,数据库的端口
    '''
    
    

    func_test7.py

    
    __author__ = "Alex Li"
    
    '''
    #*args:接受N个位置参数,转换成元组形式
    def test(*args):
    print(args)
    
    test(1,2,3,4,5,5)
    test(*[1,2,4,5,5])# args=tuple([1,2,3,4,5])
    
    print(test())
    
    #传递不固定个数参数
    def test1(x,*args):
    print(x)
    print(args)
    
    test1(1,2,3,4,5,6,7)
    '''
    
    '''
    #**kwargs:接受N个关键字参数,转换成字典的方式
    def test2(**kwargs):
    print(kwargs)
    print(kwargs['name'])
    print(kwargs['age'])
    print(kwargs['sex'])
    
    test2(name='alex',age=8,sex='F')
    test2(**{'name':'alex','age':8,'sex':'F'})
    
    
    def test3(name,**kwargs):
    print(name)
    print(kwargs)
    
    test3('alex')
    test3('alex',age=18,sex='m')
    
    '''
    
    '''
    def test4(name,age=18,**kwargs):
    print(name)
    print(age)
    print(kwargs)
    
    test4('alex',age=34,sex='m',hobby='tesla')
    '''
    
    # def test4(name,age=18,*args,**kwargs):
    def test4(*args,**kwargs):
    # print(name)
    # print(age)
    print(args)
    print(kwargs)
    logger("TEST4") #函数的调用
    
    
    def logger(source):
    print("from %s" % source)
    
    test4('alex',age=34,sex='m',hobby='tesla') #函数的定义在调用之前
    
    
  • 相关阅读:
    最详细最权威的Android 编码规范
    一款实用的工具手机软件
    一个简单的四则运算程序(优化)
    随笔
    一个简单的四则运算程序
    随手记(四)
    随手记(三)
    随手记(二)
    随手记(一)
    大学生生活中的三大痛点
  • 原文地址:https://www.cnblogs.com/netflix/p/14854368.html
Copyright © 2020-2023  润新知