Python-cod中的疑问

2024-03-28 09:18:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在读一本用Python编写的算法书,而且我对Python还是个新手。在

我不明白这个例子:

class Bunch(dict):
    def __init__(self, *args, **kwds):
        super(Bunch, self).__init__(*args, **kwds)
        self.__dict__ = self

x = Bunch(name="Jayne Cobb", position="Public Relations")
print x.name

一些问题:

  • 参数“args”和“kwds”中的*和**是什么意思?在
  • “超级”是什么意思?在
  • 在这节课中,我们是在扩展“dict”类?这是一个内置的类?在

谨致问候


Tags: nameself算法initdefargsdictclass
3条回答

*args表示:收集此列表中没有名称的所有额外参数:

def x(a, *args): pass
x(1, 2, 3)

分配a=1和{}。在

**kwargsdictkawrgs分配所有额外的参数:

^{pr2}$

分配a=1和{}。在

代码super(Bunch, self).__init__(*args, **kwds)读作:使用实例self和参数*args, **kwds调用Bunch的方法{}。这是初始化超类(docs for ^{})的标准模式

是的,dictbuilt-in data type for dictionaries。在

In this classe we are extending the "dict" class? This is a built-in class?

您实际上是在扩展基dictclass。这是Python中的一个本机类。在Python的早期版本中,您无法扩展本机类,但随着new-style classes的变化,情况发生了变化。在

What is the meaning of "super"?

函数superlets you find是给定类的父类,使用与继承相同的顺序。在

What is the meaning of the * and the ** in the parameters "args" and "kwds"?

*args用包含非命名参数的元组展开,而{}则扩展为包含命名参数的字典。管理参数数目可变的函数是a way。在

相关问题 更多 >