Python:对象中缺少“self”(不会自动传递)

Mak*_*kPo 3 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')

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

accounts = Accounts
accounts.get('k', 'v')
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

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'
Run Code Online (Sandbox Code Playgroud)

当我使用时accounts.get(key='k', value='v'),错误提示我丢失了'self'。我以前从未见过这个问题。我如何通过自我?我还以为自动通过了

MSe*_*ert 6

您应该创建一个实例:

accounts = Accounts()  # parenthesis added
Run Code Online (Sandbox Code Playgroud)

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