Hello, I'm trying to run an object on Python with ROS. When I run the code with a variable inside the object:

class lk(old_image):
....then some code
  def main(args):
        rospy.init_node('lk', anonymous=True)
        old_image = lk(old_image)

        try:
            rospy.spin()
        except KeyboardInterrupt:
            print "Shutting down"
            #cv.DestroyAllWindows()
Traceback (most recent call last):
  File "/home/aadejare/ros/omnisensor/nodes/lk.py", line 12, in <module>
    class lk(old_image):

However when I get rid of the variable inside the class I then get:

Traceback (most recent call last):
  File "***", line 67, in <module>
    main(sys.argv)
NameError: name 'main' is not defined

What would be the cause of the NameError? I tried putting in the old_image variable as a global but the same thing occurs except it identifies the variable as global. Thanks for the help

What is purpose of replacing the parent class with instance of child class at line 5? I do not understand how it would function.

Your first error message looks incomplete.

What is purpose of replacing the parent class with instance of child class at line 5? I do not understand how it would function.

Your first error message looks incomplete.

Sorry for that here's the error message

Traceback (most recent call last):
  File "/home/aadejare/ros/omnisensor/nodes/lk.py", line 12, in <module>
    class lk(old_image):
NameError: name 'old_image' is not defined

We want to modify the data so that whenever the function is called, the old image would be the previous image before it for instance, image 0 would replace image 1 at the end and ect.

This: class lk(old_image):... declares a subclass of class old_image. I'm guessing that what you want is actually to initialize an instance of lk with the old image. You do that in lk's member function def __init__(self, old_image):... Of course this is only a guess at what you intended.

By the way, why is the main function a member of class lk?

Edited 5 Years Ago by griswolf: n/a

This: class lk(old_image):... declares a subclass of class old_image. I'm guessing that what you want is actually to initialize an instance of lk with the old image. You do that in lk's member function def __init__(self, old_image):... Of course this is only a guess at what you intended.

By the way, why is the main function a member of class lk?

I want to use old_image as a variable that I can use throughout the code.
As for the main function, I am not so sure myself. I'm going to correct that.

I want to use old_image as a variable that I can use throughout the code

Then you have done the wrong thing with your class declaration. You want something like

class Lk:
  def __init__(self, old_image):
     self.old_image = old_image # whatever you really want
  # other class methods

Then you would create an instance of Lk with lk_instance = Lk(old_image)

Then you have done the wrong thing with your class declaration. You want something like

class Lk:
  def __init__(self, old_image):
     self.old_image = old_image # whatever you really want
  # other class methods

Then you would create an instance of Lk with lk_instance = Lk(old_image)

Okay, thank you but now I get this error

Traceback (most recent call last):
  File "***", line 68, in <module>
    main(sys.argv)
  File "***", line 59, in main
    old_image = lk(old_image)
UnboundLocalError: local variable 'old_image' referenced before assignment

Looks like you are really lacking the fundamentals of Python.

I'm new to Python. I kind of jumped into the deep end and am now working my way through.

This question has already been answered. Start a new discussion instead.