class MM(dict):
def __init__(self, indexed, *args, **kwargs):
super(MM, self).__init__(*args, **kwargs) #must do it.
self['name'] = 'hello, this is a value'
self.go()
def go(self, kwargs):
print kwargs #I want this to print out the kwargs
当我尝试将其初始化时,这个类怎么会产生错误?
>> m = MM()
TypeError: metaMod_Distance() takes exactly 2 arguments (1 given)
答案 0 :(得分:2)
您可能想要这样做:
def go(self, **kwargs):
print kwargs
仅接受关键字参数。因此函数调用将起作用。
此外,您必须将其他内容传递给构造函数(因为未使用的参数indexed
):
m = MM(1) #or whatever
答案 1 :(得分:2)
按以下方式修改代码:
class MM(dict):
def __init__(self, *args, **kwargs):
super(MM, self).__init__(*args, **kwargs) #must do it.
self['name'] = 'hello, this is a value'
print kwargs
# Or since you class is subclass of dict
print self
然后
m = MM() #will work
但是如果indexed是你真正需要的属性,那么在创建class时不要忘记为它指定值:
class MM(dict):
def __init__(self, indexed, *args, **kwargs):
super(MM, self).__init__(*args, **kwargs) #must do it.
self['name'] = 'hello, this is a value'
self.indexed = indexed
print kwargs
# Or since you class is subclass of dict
print self
然后:
indexed = True #since i don't know it's datatype
m = MM(indexed)
答案 2 :(得分:2)
错误很简单。你只是一个论点。您需要传入indexed
的值。
答案 3 :(得分:0)
我不确定你要做什么,但是
__init__
有indexed
作为参数,您需要在创建对象时指定。此方法也应该在从 init 调用时发送参数。所以,实际上这不会给你任何问题。
class MM(dict):
def __init__(self, indexed, *args, **kwargs):
super(MM, self).__init__(*args, **kwargs) #must do it.
self['name'] = 'hello, this is a value'
self.go(kwargs)
def go(self, kwargs):
print kwargs #I want this to print out the kwargs
m = MM('goone')