Python function of two

Advanced Functions

  1. Transfer function parameters: the angle parameter: the third parameter passing mode.

    Dynamic parameters: * args ** kwargs

    # def eat(food1,food2,food3):
    #     print(f'我请你吃:{food1},{food2},{food3}')
    # eat('蒸羊羔','蒸熊掌','蒸鹿尾')
    
    # 当给函数传入的参数数目不定时,之前的传参方式解决不了问题。
    # 万能参数,动态参数。 *args
    def eat(food1,food2,food3):
        print(f'我请你吃:{food1},{food2},{food3}')
    eat('蒸羊羔','蒸熊掌','蒸鹿尾','烧花鸭','烧企鹅')
    
    def eat(*args):  # 将实参角度:定义一个函数时,* 所有的位置参数聚合到一个元组中。
        print(args)
        print(f'我请你吃:{args}')
    eat('蒸羊羔','蒸熊掌','蒸鹿尾','烧花鸭','烧企鹅')
    
    # **kwargs
    def func(**kwargs):  # 函数的定义时:**将实参角度所有的关键字参数聚合成了一个字典,给了kwargs.
        print(kwargs)
    func(name='alex',age=84,hobby='唱跳rap篮球')
    
    # *args,**kwargs 万能参数
    def func(*args,**kwargs):
        print(args,kwargs)

    Exercise: Write a function, seeking to incoming digital variable number of arguments in function and.

    def func(lis):
        num1 = 0 
        for i in lis:
            num1 += i
        return num1
  2. * The use of magic

    # 函数中
    def func(*args,**kwargs):
        print(args)  # (1, 2, 3,'太白', 'wusir', '景女神')
        print(kwargs)
    l1 = [1, 2, 3]
    l2 = ['太白', 'wusir', '景女神']
    # func(l1,l2)
    # func(*l1,*l2)  # 当函数的执行时:*iterable 代表打散。
    func(*[1, 2, 3],*(11,22),*'fdsakl')  # 当函数的执行时:*iterable 代表打散。
    def func(*args,**kwargs):
        print(args)
        print(kwargs)
    func(**{'name':"alex"},**{'age': 73,'hobby': '吹'})
    #当函数的执行时:**dict 代表打散。
    
    
    # 函数外:处理剩余元素
    a,b,*c = [1,2,3,4,5]
    a,*c,b, = [1,2,3,4,5]
    a,*c = range(5)
    a,*c,b = (1,2,3,4,5,6)
    print(a,c,b)
  3. The final sequence of parameter angle

    *args的位置
    *args不能放在位置参数前面,a,b取不到值
    def func(*args,a,b,sex='man',):
        print(a)
        print(b)
        print(sex)
        print(args)
        # print(kwargs)
    func(1,2,4,5,6)
    
    args如果想要接收到值之前,肯定要改变sex默认参数。
    def func(a,b,sex='man',*args):
        print(a)
        print(b)
        print(sex)
        print(args)
        # print(kwargs)
    func(1,2,4,5,6)
    
    def func(a,b,*args,sex='man'):
        print(a)
        print(b)
        print(sex)
        print(args)
        # print(kwargs)
    func(1,2,4,5,6)
    func(1,2,4,5,6,sex='women')
    
    **kwargs
    位置参数,*args,默认参数,**kwargs
    def func(a,b,*args,sex='man',**kwargs,):
        print(a)
        print(b)
        print(sex)
        print(args)
        print(kwargs)
    func(1,2,4,5,6,name='太白',age=18)
    
  4. Transfer function parameters: the angle parameter: a fourth parameter passing mode (Learn)

    位置参数,*args,默认参数,仅限关键字参数,**kwargs
    def func(a,b,*args,sex='man',c,**kwargs,):
        print(a)
        print(b)
        print(sex)
        print(c)
        print(args)
        print(kwargs)
    func(1,2,4,5,6,67,c=666,name='太白',age=18,)
  5. From the perspective of function space research

    Global name space : open the file is run py, py file is stored (removal of internal function) of all correspondence between the value of the variable (address) is performed after the entire file py, will disappear.

    Temporary (partial) name space : The function is executed, in memory of a temporary open space, the function of the variable stored correspondence between values, with the end of the function and disappear.

    Built-in namespace: the INPUT, Print, and other built-in functions.

    TIM picture 20190618163607

    ![V05C~N6C[ECT6X11%CC876

    Z [GM0O ~} JA4) D2R] MZMF ($ OD2R]MZMF($O.png)

  6. The value of the order load order

    Loading order: the three above space, the first to be loaded into memory.

    Built-in namespace ---- "---- global name space" (when the function is executed) temporary namespace

    The value of the order :( principle of proximity)

  7. Scope

    Global scope: global name space, built-in namespace.

    The local scope: local name space.

  8. Built-in functions: globals, locals

    """
    此文件研究的是内置函数 globals locals
    """
    # name = 'alex'
    # l1 = [1, 2, 3]
    #
    # def func():
    #     age = '18'
    #
    # print(globals()) # 全局作用域所有的内容
    # print(locals())  # 当前位置
    
    
    # name = 'alex'
    # l1 = [1, 2, 3]
    
    # def func():
    #     age = '18'
    #     oldboy = '老男孩教育'
    #     print(globals()) # 全局作用域所有的内容
    #     print(locals())  # 当前位置的变量与值的对应关系
    #
    # func()
    
    # name = 'alex'
    # l1 = [1, 2, 3]
    #
    # def func():
    #     age = '18'
    #     oldboy = '老男孩教育'
    #     def inner():
    #         name_class = 'python23期'
    #         print(globals()) # 全局作用域所有的内容
    #         print(locals())  # 当前位置的变量与值的对应关系
    #     inner()
    # func()
  9. Higher-order functions (nested functions)

    # 例1:
    # def func1():
    #     print('in func1')
    #     print(3)
    # def func2():
    #     print('in func2')
    #     print(4)
    # func1()
    # print(1)
    # func2()
    # print(2)
    '''
    in func1
    3
    1
    in func2'
    4
    2
    '''
    
    # 例2:
    def func1():
        print('in func1')
        print(3)
    
    def func2():
        print('in func2')
        func1()
        print(4)
    
    print(1)
    func2()
    print(2)
    
    '''
    1
    in func2
    in func1
    3
    4
    2
    '''
    # # 例3:
    #
    def fun2():
        print(2)
        def func3():
            print(6)
        print(4)
        func3()
        print(8)
    
    print(3)
    fun2()
    print(5)
    
    '''
    3 2 4 6 8 5
    
    '''

Guess you like

Origin www.cnblogs.com/Jacob-yang/p/11107109.html