Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/348.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中定义类的参数_Python_Class_Parameters - Fatal编程技术网

在python中定义类的参数

在python中定义类的参数,python,class,parameters,Python,Class,Parameters,这是我运行程序时得到的代码和错误。谁能解释我犯了什么错误?像我这样在构造函数中调用一个类的函数好吗 #!/usr/bin/env python import rospy class move_turtlebot(message): def __init__(self): self.pub=rospy.Publisher('/cmd_vel', Twist, queue_size=1) self.vel=Twist() ros

这是我运行程序时得到的代码和错误。谁能解释我犯了什么错误?像我这样在构造函数中调用一个类的函数好吗

#!/usr/bin/env python

import rospy

class move_turtlebot(message):
    def __init__(self):
          self.pub=rospy.Publisher('/cmd_vel', Twist, queue_size=1)
          self.vel=Twist()
          rospy.loginfo("initiated!!")
         self.move(message)
    def move(self,message):
          if message=="STOP":
              rospy.loginfo("inside stop")
              self.vel=Twist()
              self.pub.publish(vel)
          elif message=="LEFT":
        #rest of code continues 
if __name__=="__main__":

   rospy.init_node('move_turtlebot')
   rospy.loginfo("here!")

   while not rospy.is_shutdown():
       ahead=move_turtlebot("FORWARD")
错误为:NameError:在定义类的行中未定义名称“message”

class move_turtlebot(message): # this means inheritance from a class message which u dont have or didnt define
对于要具有参数的类,可以在构造函数中这样定义它们(尽管存在变化):

对于要具有参数的类,可以在构造函数中这样定义它们(尽管存在变化):


在类定义中,您正在使用消息:


类移动\u turtlebot(消息):在类定义中,您正在使用消息:


类移动turtlebot(消息):在您的
\uuu init\uuuu
方法中,
消息
尚未定义。另外,
elif
未正确缩进您的类
move\u turtlebot
继承自
message
,但该类未定义/导入我创建了类“ahead=move\u turtlebot”(“FORWARD”)的对象,消息变量是字符串“FORWARD””“这还不够吗。定义/导入类意味着什么。在您的
\uuuu init\uuu
方法中,
消息
尚未定义。另外
elif
没有正确缩进您的类
move\u turtlebot
继承自
message
,但是这个类没有定义/导入我创建了一个类“ahead=move\u turtlebot”(“FORWARD”)的对象,消息变量是一个字符串“FORWARD”,这还不够。定义/导入类意味着什么。
class move_turtlebot:
    def __init__(self,message):