ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Revision history [back]

The Problem is in how you generate the list of objects to begin with. The * multiply will link each copy to the same instance. Thus, you Change both objects with the same call.

You should generate the list by either Clearing it and appending the appropriate GestureDetectorHuman object or use something else for generating the list, as e.g.

self.publisher_msg.gesture_list = [ GestureDetectorHuman() for _ in range(human_list_length)]

The Problem is in how you generate the list of objects to begin with. The * multiply will link each copy to the same instance. Thus, you Change both objects with the same call.

You should generate the list by either Clearing it and appending the appropriate newly generated GestureDetectorHuman object or use something else for generating the list, as e.g.

self.publisher_msg.gesture_list = [ GestureDetectorHuman() for _ in range(human_list_length)]

The Problem is in how you generate the list of objects to begin with. The * multiply will link each copy to the same instance. Thus, you Change both objects with the same call.

You should generate the list by either Clearing it and appending the appropriate newly generated GestureDetectorHuman object or use something else for generating the list, as e.g.

self.publisher_msg.gesture_list = [ GestureDetectorHuman() for _ in range(human_list_length)]

EDIT

Strange, I just tested this with a simple python class

class test(object):
    a = 0

and the behaviour is as expected.

a = [test()] * 5

creates references to the same object (i.e. changing a[0].a changes also a[1].a etc), whereas

b = [ test() for _ in range(5) ]

creates 5 objects, and changing works.

You could check by simply print a and see to which id the single elements are pointing.

Why this DOES work for simple, immutable types in python, is explained in several places. One that I like (though I'm not the python expert, and am not sure if this is the full true solution) is a comment in this StackOverflow question:

Everything is an object, and variables are names that we assign to objects (not the other way around!) So if you do a = b = c = 1; a +=1, then everything gets assigned to the object 1, and then a gets assigned to the object 2. With mutable objects like lists, dicts and sets, you can change the object itself, which will be seen by all names assigned to that object, but with ints, strings, tuples, and other immutable objects, all you can do is assign the name to a different object.