【发布时间】:2016-01-25 15:51:53
【问题描述】:
我正在尝试创建一个名为 ListenerVilma 的类,它有两种方法:“Clock_”和“Diagnostics_”。尽管如此,这两种方法都将调用内部函数。以下代码显示了我尝试实现上述行为,但是当我调用 ListenerVilma.Clock_() 时,得到以下错误:
TypeError: 未绑定的方法 Clock_() 必须以 ListenerVilma 实例作为第一个参数调用(什么都没有)
应该如何创建我的类ListenerVilma???
谢谢。
#!/usr/bin/env python
import rospy
from rosgraph_msgs.msg import Clock
from diagnostic_msgs.msg import DiagnosticArray
class ListenerVilma:
"""Class that listens all topics of the file vilmafeagri"""
def Clock_(self):
"""Method that listens the topic /clock if the file vilmafeagri"""
def __init__(self):
self.listener()
def callback(self, clock):
print clock
def listener(self):
rospy.Subscriber('clock', Clock, self.callback)
def Diagnostics_(self):
"""Method that listen the topic /diagnostics from rosbag file vilmafeagri"""
def __init__(self):
self.listener()
def callback(self, diagnostics):
print diagnostics
def listener(self):
rospy.Subscriber('diagnostics', DiagnosticArray, self.callback)
if __name__ == '__main__':
rospy.init_node('listener', anonymous=True)
ListenerVilma.Clock_()
rospy.spin()
【问题讨论】:
-
时钟是类还是方法?它看起来像一个带有
__init__方法的类,但你使用了def -
假设它们是类,为什么它们嵌套在 ListenerVilma 中?几乎没有理由在 Python 中嵌套类。也许 ListenerVilma 应该是一个模块。