Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么这个Python方法没有";“自我”吗;?_Python_Oop - Fatal编程技术网

为什么这个Python方法没有";“自我”吗;?

为什么这个Python方法没有";“自我”吗;?,python,oop,Python,Oop,当我尝试初始化时,这个类为什么会产生错误 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):

当我尝试初始化时,这个类为什么会产生错误

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)
只接受关键字参数。因此,函数调用将起作用

此外,还必须将其他内容传递给构造函数(因为未使用的参数
索引了
):


按以下方式修改代码:

m = MM(1) #or whatever
然后

但若索引是您真正需要的属性,那个么在创建类时不要忘记为其指定值:

m = MM() #will work
然后:


错误很简单。你少了一个论点。您需要为
索引的
传递一个值我不确定您到底想做什么,但是

  • MM的
    \uuuu init\uuuu
    已将
    索引为一个参数,您需要在创建对象时指定该参数
  • 当从init调用时,go方法也应该发送一个参数。所以,实际上这不会给你带来任何问题

    indexed = True #since i don't know it's datatype
    m = MM(indexed)
    

  • “为什么这个Python方法没有‘self’?”与您所问的实际问题有什么关系?metaMod_Distance()
    来自哪里?我冒昧地对您的代码进行了双缩进。但这让我想知道是否有一种方法可以结束列表,这样代码就可以与页边空白齐平。传播消息!
    m = MM() #will work
    
    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)
    
    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')