这种情况经常发生。一个函数返回一个我无法读取的对象。这里:
discoverer = GstPbutils.Discoverer()
discoverer.connect('discovered', on_discovered)
info = discoverer.discover_uri(self.loaded_file)
print(vinfo.get_tags())
返回这个:
<Gst.TagList object at 0x7f00a360c0a8 (GstTagList at 0x7f00880024a0)>
但是当我尝试这样做时:
tags = vinfo.get_tags()
for tag in tags:
print (tag)
我明白了:
TypeError: 'TagList' object is not iterable
但是当我读到 the doc of this data structure ,我好像明白了是……列表?除了告诉我如何获取标签之外,有人可以告诉我如何阅读那些文档吗?另外,我是否缺少一些内省(introspection)方法和工具,我可以用它们来发现我遇到的对象是什么,以及它们是如何工作的?
最佳答案
这都是假设,因为我从未将 python 与 GStreamer 一起使用:
根据文档 - 是的,它是说它的列表..但这可以表示为内部结构..记住 python 绑定(bind)只是..绑定(bind) - 它的工作方式与以下类似(如果没有以更好的方式实现) C.. 你在C with tags to iterate them 做什么? .. 但不要问我是怎么发现的——你必须查看文档以检查所有可用的函数。
你必须要聪明,想想你正在使用的对象是如何实现的——以及你知道它代表什么的事实。我的意思是——这是标签列表,当每个标签都有不同的类型时——一个是字符串,另一个是 int 等等。你不能轻易地迭代它。
所以我认为你有两个选择——根据你想用标签做什么..
1,序列化为字符串并使用它:
我不确定,但在 C 中有 to_string这可能与 to_string 中的功能相同在 python 中 - 如果您只对标签名称感兴趣,请尝试这样做......或者它返回的任何内容。
2、使用内置foreach及其回调定义:
tags = vinfo.get_tags()
tags.foreach(my_callback, self)
在你的回调中:
def my_callback(list, tag, user_data):
print(tag)
#do whatever you want with list
#not sure how to use casting in python:
YourClass ptr = user_data
ptr.your_method(whatever, tag);
关于Python 类型错误 : 'TagList' object is not iterable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38090102/