Why doesn't `map()`in Python work here?

I narrowed down a bug in my own program, and found that the following works, but if I swap the enabled and disabled code, I get a blank screen instead of a window showing my webcam:

    def create_pipeline(self, gst_element_names):

        gst_elements = list(map(Gst.ElementFactory.make, gst_element_names))
        gst_pipeline = Gst.Pipeline()

        # for some reason, map() doesn't work here
        #map(gst_pipeline.add, gst_elements)

        # ... so
        for element in gst_elements:
            gst_pipeline.add(element)

        for i in range(0, len(gst_elements) - 1):
            gst_elements[i].link(gst_elements[i + 1])

        sink = gst_elements[-1]
        self.video_container.append(sink.props.widget)

        sink.set_state(Gst.State.READY)
        gst_pipeline.set_state(Gst.State.PLAYING)
help(Gst.Pipeline)
...
 |  ----------------------------------------------------------------------
 |  Data and other attributes inherited from Bin:
 |  
 |  add = gi.FunctionInfo(add)
help(Gst.Pipeline.add)
...
add = class FunctionInfo(CallableInfo)
 |  Method resolution order:
 |      FunctionInfo
 |      CallableInfo
 |      BaseInfo
 |      builtins.object
 |  
 |  Methods defined here:

I might be missing something obvious here, but would appreciate if anyone could help to shed light on the difference between the two. Thanks : )

Is it because the add method is secretly a class, and its invocation changes future references to it from the Gst.PIpeline object? In the example with map(), I used the same reference for each invocation, so that might explain the difference.

This has nothing to do with Gst.Pipeline, it’s how Python map() works:

$ python
>>> a = []
>>> map(a.append, [1, 2, 3])
<map object at 0x7f98cca4d880>
>>> a
[] 

map() returns an iterator, but nobody “iterates” it, you must do something like

>>> list(map(a.append, [1, 2, 3]))
[None, None, None]
>>> a
[1, 2, 3]

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.