这是我的代码:
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.get(key='k', value='v')
时,错误显示我缺少'self'
。我以前从未见过这个问题。我如何通过自我?我以为它是自动通过的。
答案 0 :(得分:2)
您应该创建一个实例:
accounts = Accounts() # parenthesis added
否则,您访问类上的方法(其行为类似于普通的python函数)而不是实例(其中self被隐式传递)。
答案 1 :(得分:2)
您将accounts
设置为Accounts
类的别名,但您需要将初始化作为实例< / em> of Accounts:
accounts = Accounts()
否则,它被称为静态方法,因此没有&#34; self&#34;可以传递(因为self
始终是类的实例)