【发布时间】:2021-11-13 06:00:06
【问题描述】:
我有两个类,在第二个类中我定义了一个方法,该方法从第一个类中实例化一个对象,为其属性分配一个值,然后打印它。
问题是当我使用以下方法调用函数时:
test.Pprint(5)
我得到了错误。
TypeError: Pprint() missing 1 required positional argument: 'x'
我的代码:
class node():
def __init__(self, test):
self.test = test
class test():
def Pprint(self, x):
rootnode = node(x)
print(rootnode.test)
当我删除关键字self 时,一切都按预期进行。据我所知self 不应被视为论点。有什么问题?
【问题讨论】:
-
self是参数。你没有传入x。test是一个类,而不是一个保险。致电test().Pprint(5) -
@MadPhysicist
insurance->instance -
@MattDMo。自动更正很有趣:)
标签: python class methods typeerror self