Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.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对象中缺少“self”(未自动传递)_Python_Error Handling - Fatal编程技术网

Python对象中缺少“self”(未自动传递)

Python对象中缺少“self”(未自动传递),python,error-handling,Python,Error Handling,这是我的密码: from xml.etree.ElementTree import ElementTree from xml.etree.ElementTree import Element import xml.etree.ElementTree as etree import os class Accounts: def __init__(self): self.file_path = os.path.join('lib', 'accounts.xml')

这是我的密码:

from xml.etree.ElementTree import ElementTree
from xml.etree.ElementTree import Element
import xml.etree.ElementTree as etree
import os

class Accounts:
    def __init__(self):
        self.file_path = os.path.join('lib', 'accounts.xml')

    def get(self, key, value):
        tree = ElementTree.parse(self.file_path)
        print(tree)

accounts = Accounts
accounts.get('k', 'v')
我得到这个错误:

Traceback (most recent call last):
  File ".\XML_Build.py", line 15, in <module>
    accounts.get('k', 'v')
TypeError: get() missing 1 required positional argument: 'value'

当我使用accounts.getkey='k',value='v'时,错误显示我缺少“self”。我以前从未见过这个问题。我怎样通过赛尔夫考试?我以为它是自动通过的

您应该创建一个实例:

accounts = Accounts()  # parenthesis added

否则,您将在类上访问该方法,该类的行为类似于普通python函数,而不是在隐式传递self的实例上访问该方法。

您应该创建一个实例:

accounts = Accounts()  # parenthesis added
否则,您将访问类上的方法,该类的行为类似于普通的python函数,而不是隐式传递self的实例。

您将accounts设置为accounts类的别名,但需要将其初始化为accounts的实例:

accounts = Accounts()
否则,它将作为静态方法调用,因此不能传递任何self,因为self始终是类的实例

您将accounts设置为accounts类的别名,但需要将其初始化为accounts的实例:

accounts = Accounts()
否则,它将作为一个静态方法调用,因此不能传递self,因为self始终是类的实例