zoukankan      html  css  js  c++  java
  • python 函数参数

    # 函数的参数分为两种:
    # 形参:在定义阶段括号内指定的参数,在定义阶段不暂用内存空间,相当于变量名
    # 实参:在调用阶段括号内传入的值,相当于值
    # 在调用阶段,实参的值会绑定给形参,在调用结束后解除绑定
    def foo(x, y):
    print(x, y)

    foo(1, 2)

    ####################################################

    # 在python中参数的分类:
    # 1、位置参数:按照从左到右的顺序依次定义的参数
    # 通常用于经常变化的参数
    # 位置形参:定义完之后,必须被传值,多一个少一个都不行
    # 位置实参:与形参一一对应传值
    def foo1(x, y):
    print(x, y)

    foo1(1 ,2)

    ##############################################################################

    # 关键字参数:在函数调用时,按照key=value的形式定义的实参
    # 特点:指名道姓的给形参传值,不再依赖于位置
    # 注意:关键字实参必须在位置实参的后面
    # 不能为用一个参数赋值多次
    def foo2(x, y):
    print(x, y)

    foo1(y=1 ,x=2)

    ############################################################################

    # 3、默认参数:在函数定义阶段,就已经为形参赋值了
    # 通常用于不变化的情况
    # 默认参数必须放到位置形参后面
    # 默认参数的值只在定义时被赋值一次,定义之后的修改都不会影响默认参数的值
    # 默认参数的值通常应该是不可变类型
    def foo3(x, y=1):
    print(x,y)

    foo3(1, 6) # 输出结果:1 6
    foo3(5) # 输出结果:5 1

    #############################################################
    # 默认参数的值只在定义时被赋值一次,定义之后的修改都不会影响默认参数的值
    res = 1
    def foo5(x, y=res):
    print(x, y)

    res = 10
    foo5('aaaaa') # 输出结果:aaaaa 1

    ###############################################################

    4、可变长参数:在调用函数时,实参值的个数不固定
    # 实参的形式:位置实参、关键字实参
    对于实参个数不确定的情况,形参的解决方案:
    1)、*(按照位置实参传值的情况)
    输出结果中,*部分为元祖类型
    def foo6(x, y, *z):
    print(x, y, z)
    foo6(1, 2, 3, 4, 5, 6)
    输出结果:1 2 (3, 4, 5, 6)

    def foo6(x, y, *z):
    print(x, y, z)
    foo6(1, 2, *[3, 4, 5, 6]) 等同于 foo6(1, 2, 3, 4, 5, 6)
    输出结果:1 2 (3, 4, 5, 6)

    def foo6(x, y, *z):
    print(x, y, z)
    foo6(*[1, 2, 3, 4, 5, 6]) 等同于 foo6(1, 2, 3, 4, 5, 6)
    输出结果:1 2 (3, 4, 5, 6)

    def foo6(x, y, *z):
    print(x, y, z)
    foo6(*{'a':5, 'b':7, 'c':8, 'd':0})
    输出结果:a b ('c', 'd')

    def foo6(x, y, *z):
    print(x, y, z)
    foo6(*(1, 2, 3, 4, 5))
    输出结果:1 2 (3, 4, 5)

    def foo6(name, age):
    print(name, age)

    foo6(*{'name':'keke', 'age':7})
    输出结果:name age

    2)、 **(按照关键字实参传值的情况)
    **部分,实参类型为字典类型
    输出结果中,**部分为字典类型
    def foo6(x, y, **z):
    print(x, y, z)

    foo6(y=1, x=2, a=3, b=4, c=5)
    输出结果:2 1 {'a': 3, 'b': 4, 'c': 5}

    def foo6(x, y, **z):
    print(x, y, z)

    foo6(y=1, x=2, **{'a':5, 'b':7, 'c':8})
    输出结果:2 1 {'a': 3, 'b': 4, 'c': 5}

    def foo6(x, y, **z):
    print(x, y, z)

    foo6(y=1, **{'a':5, 'b':7, 'c':8})
    输出结果:TypeError: foo6() missing 1 required positional argument: 'x'

    def foo6(name, age):
    print(name, age)

    foo6(**{'name':'keke', 'age':7})
    输出结果:keke 7

    def bar(x, y, z):
    print(x, y, z)

    def foo6(*args, **kwargs):
    bar(*args, **kwargs)

    foo6(1, y=2, z=3)
    输出结果:1 2 3

    ############################################################################
    命名关键字参数:指的是定义在*后的参数,该参数必须被传值(除非它有默认值),
    而且必须按照key=value的形式传值

    def bar(x, y, *, m, n):
    print(x, y, m, n)

    bar(1, 2, m=3, n=5)
    输出结果:1 2 3 5








  • 相关阅读:
    Find the Smallest K Elements in an Array
    Count of Smaller Number
    Number of Inversion Couple
    Delete False Elements
    Sort Array
    Tree Diameter
    Segment Tree Implementation
    Java Programming Mock Tests
    zz Morris Traversal方法遍历二叉树(非递归,不用栈,O(1)空间)
    Algorithm about SubArrays & SubStrings
  • 原文地址:https://www.cnblogs.com/keqing1108/p/13448789.html
Copyright © 2011-2022 走看看