以下是我的类定义:为什么它说类型错误:“集群”对象不是可调用,即使调用(集群)给出真实
class logline:
def __init__(self,t,cmp,msg):
self.t = t
self.cmp = cmp
self.msg = msg
class cluster:
clusters = []
def __init__(self,status,log):
self.status = status
self.children = []
self.eventlogs = []
self.rep_msg = log.msg
self.addLog(log)
self.prev = None
if(status == 'root'):
cluster.clusters.append(self)
def prev(self):
return self.prev
def print_children(self):
for child in range(0,len(self.children)):
print(self.children[child].rep_msg)
self.children[child].print_logs()
def print_logs(self):
for log in self.eventlogs:
print(log.msg)
def add_child(self,status,log):
temp = cluster(status,log)
self.children.append(temp)
temp.prev=self
return temp
def addLog(self,log):
self.eventlogs.append(log)
现在,树是我的根群集节点
tree = cluster('root',log1)
和prev是我的孩子群集节点添加到树
tree = tree.add_child('child',log6)
当我尝试: tree = tree.prev()
我应该回去树,但它给我的错误:
tree = tree.prev()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'cluster' object is not callable
在另一方面: callable(cluster)
计算结果为真
我的类定义的基础关闭的:How can I implement a tree in Python? Are there any built in data structures in Python like in Java?
我已经四处搜索,但似乎无法找到任何符合我的情况的东西
在此先感谢
编辑: 所以,我在python绝对的初学者,我应该有可能导致与
>>> print(tree)
<__main__.cluster object at 0x02AF8590>
>>> print(tree.prev)
<__main__.cluster object at 0x02AEA270>
我假设,因为我得到不同的位置对于这两个陈述,prev已被设置为 但我无法返回到我的父节点
return self.prev
顺便说一句,如果你在每个方法定义之后放一个空行,那么你的代码会更容易阅读。此外,您应该使用[PEP-0008](https://www.python.org/dev/peps/pep-0008/)命名约定并为您的类指定CamelCase名称,例如'Cluster'和'Logline'或' LogLine'。 –