参数详解

 

/*
目录: 参数
    一: 实参
        1 位置
        2 关键字
        3 原子化
    二: 形参
        1 无默认参数
        2 默认参数
            (a) 普通
            (b) 空
            (c) None
    三: 可变参数
    四: 可变命名参数
    五: 书写顺序
*/

 

 

 

 

一: 实参
  1 位置

# 位置
def test(title, msg):
    print(title, msg)

if __name__ == '__main__':
    test("hello", "demon")  # hello demon
    test("hello", "demon")  # demon hello

 

  2 关键字

# 关键字
def test(title, msg):
    print(title, msg)

if __name__ == '__main__':
    test("hello", msg="demon")          # hello demon
    test(title="hello", msg="demon")    # hello demon
    test(msg="demon", title="hello")    # hello demon
    test(title = "demon", "hello") # SyntaxError: positional argument follows keyword argument

 

  3 原子化

# 入参: 原子化
def user_info(*args):
    print(type(args), args)

if __name__ == '__main__':
    user_info(22, "TOM", [1, "abc"], {"name": "Jack"})  # <class 'tuple'> (22, 'TOM', [1, 'abc'], {'name': 'Jack'})

    l = [1, 2, 3]
    s = "abc"
    user_info(l, s)     # <class 'tuple'> ([1, 2, 3], 'abc')
    # # 入参: 原子化
    user_info(*l, s)    # <class 'tuple'> (1, 2, 3, 'abc')
    user_info(*l, *s)  # <class 'tuple'> (1, 2, 3, 'a', 'b', 'c')   

 

二: 形参
  1 无默认参数

def test(title):
    print(title)

if __name__ == '__main__':
    test("hello")   # hello

 

  2 默认参数
    (a) 普通

# 默认值
def test(title, msg="demon"):
    print(title, msg)

if __name__ == '__main__':
    test("hello")           # hello demon
    test("hello", "123")    # hello 123

 

    (b) 空

# 各数据类型初始化为空, 其布尔类型为false
if __name__ == '__main__':
    n = int()
    s = str()
    s2 = ""
    l = list()
    l2 = []
    d = dict()
    se3 = set()

    print(type(n), n, bool(n))              # <class 'int'> 0 False
    print(type(s), s, len(s), bool(s))      # <class 'str'>  0 False
    print(type(s2), s, len(s2), bool(s2))   # <class 'str'>  0 False

    print(type(l), l, len(l), bool(l))      # <class 'list'> [] 0 False
    print(type(l2), s, len(l2), bool(l2))   # <class 'list'>  0 False

    print(type(d), s, len(d), bool(d))          # <class 'dict'>  0 False
    print(type(d), s, len(d), bool(d))          # <class 'dict'>  0 False
    print(type(se3), s, len(se3), bool(se3))    # <class 'set'>  0 False

 

# 形参为空
def test(n = int(), s = str(), s1 = "", l = list()):
    print(type(n), n, bool(n))
    print(type(s), s, len(s), bool(s))
    print(type(s1), s, len(s1), bool(s1))
    print(type(l), l, len(l), bool(l))
    
if __name__ == '__main__':
    test()
'''
<class 'int'> 0 False
<class 'str'>  0 False
<class 'str'>  0 False
<class 'list'> [] 0 False
'''

 

    (c) None

# 形参为None
def test(args = None):
    print(type(args), args)

if __name__ == '__main__':
    test("a")               # <class 'str'> a
    test(12)                # <class 'int'> 12
    test([1, 2])            # <class 'list'> [1, 2]
    test({"name":"Tom"})    # <class 'dict'> {'name': 'Tom'}
    test(12, "a")   # TypeError: test() takes from 0 to 1 positional arguments but 2 were given

 


三: 可变参数

# 可变参数
def user_info(*args):
    print(type(args), args)


if __name__ == '__main__':
    user_info(22)                       # <class 'tuple'> (22,)
    user_info(22, "TOM")                # <class 'tuple'> (22, 'TOM')
    user_info(22, "TOM", [1, "abc"])    # <class 'tuple'> (22, 'TOM', [1, 'abc'])
    user_info(22, "TOM", [1, "abc"], {"name": "Jack"})  # <class 'tuple'> (22, 'TOM', [1, 'abc'], {'name': 'Jack'})

 

四: 可变命名参数

# 可变命名参数
def user_info(**kwargs):
    print(type(kwargs), kwargs) # <class 'dict'> {'name': 'Jack', 'age': 33}

if __name__ == '__main__':
    user_info(name="Jack", age=33)  # <class 'dict'> {'name': 'Jack', 'age': 33}

    d = {"name":"Tom", "age":11}    
    user_info(**d)  # <class 'dict'> {'name': 'Tom', 'age': 11}

 


五: 书写顺序

# 书写顺序
#   无默认参数; 默认参数; 可变参数; 可变命名参数
#   value; name=value; *args, **kwargs    

 

# 组合
def register(name, email, **kwargs):
    print("name:%s, age:%s, others:%s" %(name, email, kwargs))

if __name__ == '__main__':
    register("demon", "1@1.com", addr="BeiJin", job="software") 
    # name:demon, age:1@1.com, others:{'addr': 'BeiJin', 'job': 'software'}

 

 

 

posted @ 2023-04-27 21:40  火焰马  阅读(15)  评论(0编辑  收藏  举报