【问题标题】:Python classes: How should a built itPython 类:应该如何构建它
【发布时间】: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 应该是一个模块。

标签: python class ros


【解决方案1】:

错误在ListenerVilma.Clock_() 的第 41 行,这里你直接使用你的类的方法,所以没有隐式参数被传递,并且需要一个 ListenerVilma 的实例。解决方案是ListenerVilma().Clock_() 这首先创建你的类的一个实例,然后从实例调用它的 Clock_ 方法。

除此之外,你的类构造很奇怪,__init__ 用于初始化一个class,基本的类构造是这样的

class Foo:
    """example class"""

    def __init__(self,*argv,**karg):
        """initialize this class"""
        #do something with argv and/or karg according to the needs
        #for example this
        print "init argv", argv
        print "init karg", karg
        self.ultimate=42

    def do_stuff(self):
        """this method do something"""
        print "I am doing some stuff"
        print "after 7.5 million years of calculations The Answer to the Ultimate Question of Life, the Universe, and Everything is: ", self.ultimate

    def do_some_other_stuff(self,*argv,**karv):
        """this method do something else"""
        print "method argv", argv
        print "method karg", karg

# basic usage            
test = Foo(1,2,3,key_test=23)
test.do_stuff()
test.do_some_other_stuff(7,8,9,something=32)
test.ultimate = 420
test.do_stuff()

我不太确定你的意图是什么,但你将 Clock_Diagnostics_ 构建为一个类,但它们不是,而且现在它们什么都不做,如果你希望它们自己成为类做

class Clock_:
    def __init__(self):
        self.listener()

    def callback(self, clock):
        print clock

    def listener(self):
        rospy.Subscriber('clock', Clock, self.callback)

Diagnostics_ 一样,我看不出listener 方法的原因,所以我会把它的作用放在__init__ 中,但也许rospy 需要它?我不知道,但从外观上看,它应该用作

rospy.init_node('listener', anonymous=True)
Clock_()
Diagnostics_()
rospy.spin()

【讨论】:

    【解决方案2】:

    Clock_ 方法不属于该类;这是一种“实例”方法。 有两种选择

    • 在main函数中:创建ListenerVilma的实例:listener = ListenerVilma(),或者
    • ListenerVilma类中:用@classmethod注解方法,并使类继承自对象:class ListenerVilma(object):。但请记住,方法中的第一个参数将是对类的引用,而不是对实例的引用。

    【讨论】:

      【解决方案3】:

      以下代码可以更好地执行我想要的行为。 :)

      class ListenerVilma:
      
          def CLOCK(self):
      
              def clock_sub():
                  rospy.Subscriber('clock', Clock, clock_callback)
      
              def clock_callback(clock):
                  print clock
      
              clock_sub()
      
      
          def DIAGNOSTICS(self):
      
              def diagnostics_sub():
                  rospy.Subscriber('diagnostics', DiagnosticArray, diagnostics_callback)
      
              def diagnostics_callback(diagnostics):
                  print diagnostics
      
              diagnostics_sub()
      
      if __name__ == '__main__':
          rospy.init_node('listener', anonymous=True)
          myVilma = ListenerVilma()
          myVilma.CLOCK()
          myVilma.DIAGNOSTICS()
          rospy.spin()
      

      【讨论】:

        猜你喜欢
        • 2019-02-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-04-24
        • 2018-07-17
        • 1970-01-01
        相关资源
        最近更新 更多