Python中的“*”和“**”

 1.接受任意长度形参,组成turple

def function(*args):
    # type(args)==turple
    # args==(1, 2, 3, 4)
    print(args)
    ant=0
    for i in range(len(args)):
        ant+=args[i]
    return ant

print(function(1,2,3,4))  # 10

 2.接受任意长度形参,组成dict

def function(**args):
    # type(args)==<class'dict'>
    print(type(args))
    # {'a': 1, 'b': 2, 'c': 3, 'd': 4}
    print(args)
    ants = 0
    for key in args:
        ants+=args[key]
    return ants
 
print(function(a=1,b=2,c=3,d=4))   # 10

# 注意这种方法是错误的
print( function({"a":1,"b":2,"c":3,"d":4}) )
# TypeError: function() takes 0 positional arguments but 1 was given
def my_function(**kwargs):
    # {'a': 1, 'b': 2}
    print(kwargs)

my_dict = {'a': 1, 'b': 2}
my_function(**my_dict)

 3. 拿出参数

在list中 

def sample_function(a, b, c):
    print(a, b, c)

list = [1, 2, 3]

sample_function(*list)


list2=[10,list,20]
list3=[10,*list,20]
# [10, [1, 2, 3], 20]
print(list2)
# [10, 1, 2, 3, 20]
print(list3)

在turple中 

def sample_function(a, b, c):
    print(a, b, c)

list = (1, 2, 3)

sample_function(*list)

list2=(10,list,20)
list3=(10,*list,20)
# (10, (1, 2, 3), 20)
print(list2)
# (10, 1, 2, 3, 20)
print(list3)

4.函数传参对号入座

def test(a, b):
    print(f"a={a}, b={b}")

d = {'b': 200, 'a':30}
test(**d)

参数和形参名称不对应,报错 

def test(c,d):
    print(c," ",d)
d = {'b': 100, 'a':10}
test(**d)
"""
File "d:\WorkSpace\MachineLearning\d2l_learn\mainTest2.py", line 4, in <module>
    test(**d)
TypeError: test() got an unexpected keyword argument 'b'
"""

题外话:看到*args和**kwds既不害怕了

class Person(object):
    def __init__(self) -> None:
        pass
    def __call__(self, *args: Any, **kwds: Any) -> Any:
        pass
    def __new__(cls) :
        pass

相关推荐

  1. Python“*”“**”

    2024-06-14 20:02:03       30 阅读
  2. Pythonloguru配置使用

    2024-06-14 20:02:03       52 阅读
  3. Pythonnewcall方法

    2024-06-14 20:02:03       44 阅读
  4. python模块

    2024-06-14 20:02:03       50 阅读
  5. python错误异常

    2024-06-14 20:02:03       36 阅读
  6. Python,type() isinstance() 区别

    2024-06-14 20:02:03       44 阅读
  7. Python__init____new__

    2024-06-14 20:02:03       31 阅读
  8. Python *args **kwargs

    2024-06-14 20:02:03       39 阅读
  9. Python错误异常处理

    2024-06-14 20:02:03       35 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-14 20:02:03       91 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-14 20:02:03       97 阅读
  3. 在Django里面运行非项目文件

    2024-06-14 20:02:03       78 阅读
  4. Python语言-面向对象

    2024-06-14 20:02:03       88 阅读

热门阅读

  1. 前端开发之HTTP协议认识

    2024-06-14 20:02:03       33 阅读
  2. Web前端在深圳:探索技术与创新的融合之地

    2024-06-14 20:02:03       30 阅读
  3. 系统编程 - kill,alarm,read,write

    2024-06-14 20:02:03       25 阅读
  4. 学习笔记——交通安全分析04

    2024-06-14 20:02:03       26 阅读
  5. 在Android Studio中将某个文件移出Git版本管理

    2024-06-14 20:02:03       28 阅读
  6. 好用的国内镜像源

    2024-06-14 20:02:03       19 阅读
  7. 力扣刷题总结 -- 数组26

    2024-06-14 20:02:03       26 阅读
  8. Linux之history历史指令查看

    2024-06-14 20:02:03       33 阅读
  9. Leetcode:合并两个有序链表

    2024-06-14 20:02:03       29 阅读
  10. ubuntu20.04 minio 安装为服务

    2024-06-14 20:02:03       25 阅读
  11. 查看ubuntu中的分区是什么类型的

    2024-06-14 20:02:03       29 阅读