Python 这段特定的代码是如何工作的?
以下内容摘自流行的python repo,即youtube dl。为了努力成为一名更好的程序员,我浏览了这一部分,我很难理解它 特别是最后一个方法,以及如果在列表中找不到ie_键,它如何不进入无限递归 以及第一种方法中的实例比较Python 这段特定的代码是如何工作的?,python,Python,以下内容摘自流行的python repo,即youtube dl。为了努力成为一名更好的程序员,我浏览了这一部分,我很难理解它 特别是最后一个方法,以及如果在列表中找不到ie_键,它如何不进入无限递归 以及第一种方法中的实例比较 我理解通常的实现是这样的:isinstance('hello',str),但是type()怎么可能是类型呢?此外,将ie对象与类型进行比较有什么意义?这肯定会导致无限递归。在递归调用之间,self似乎没有发生任何更新。_ies_实例,因为递归依赖于这种情况,它将继续 也
我理解通常的实现是这样的:isinstance('hello',str),但是type()怎么可能是类型呢?此外,将ie对象与类型进行比较有什么意义?这肯定会导致无限递归。在递归调用之间,
self似乎没有发生任何更新。_ies_实例
,因为递归依赖于这种情况,它将继续
也许这是一个bug,但是代码从来没有出现过ie_key
不在字典中的情况
至于你对类型的混淆
,这是(一次伟大的阅读)的结果<代码>类型既充当返回对象类型的“函数”,也充当创建新类型的类(使用更多参数调用时)
您可能希望检查某个对象是否是类型的实例的一个原因是查看某个对象是否是元类:
def add_info_extractor(self, ie):
"""Add an InfoExtractor object to the end of the list."""
self._ies.append(ie)
if not isinstance(ie, type):
self._ies_instances[ie.ie_key()] = ie
ie.set_downloader(self)
def get_info_extractor(self, ie_key):
"""
Get an instance of an IE with name ie_key, it will try to get one from
the _ies list, if there's no instance it will create a new one and add
it to the extractor list.
"""
ie = self._ies_instances.get(ie_key)
if ie is None:
ie = get_info_extractor(ie_key)()
self.add_info_extractor(ie)
return ie
由于对象
是“所有新样式类的基础”(),因此它还充当元类(如上所示)。这肯定会导致无限递归。在递归调用之间,self似乎没有发生任何更新。_ies_实例
,因为递归依赖于这种情况,它将继续
也许这是一个bug,但是代码从来没有出现过ie_key
不在字典中的情况
至于你对类型的混淆
,这是(一次伟大的阅读)的结果<代码>类型
既充当返回对象类型的“函数”,也充当创建新类型的类(使用更多参数调用时)
您可能希望检查某个对象是否是类型的实例的一个原因是查看某个对象是否是元类:
def add_info_extractor(self, ie):
"""Add an InfoExtractor object to the end of the list."""
self._ies.append(ie)
if not isinstance(ie, type):
self._ies_instances[ie.ie_key()] = ie
ie.set_downloader(self)
def get_info_extractor(self, ie_key):
"""
Get an instance of an IE with name ie_key, it will try to get one from
the _ies list, if there's no instance it will create a new one and add
it to the extractor list.
"""
ie = self._ies_instances.get(ie_key)
if ie is None:
ie = get_info_extractor(ie_key)()
self.add_info_extractor(ie)
return ie
由于对象是“所有新样式类的基础”(),因此它还充当元类(如上所示)。我认为这避免了无限递归的原因是它根本就不会递归!仔细观察:
>>> isinstance(1, type)
False
>>> isinstance("", type)
False
>>> isinstance({}, type)
False
>>> isinstance((), type)
False
>>> type(object) == type
True
>>> isinstance(object, type)
True
>>> isinstance(object(), type)
False
>>> class a(): pass
...
>>> isinstance(a, type)
False
>>> isinstance(a(), type)
False
请注意,get\u info\u提取器
是一个方法,它调用一个非方法函数,该函数恰好也被命名为get\u info\u提取器
,因此它不调用自身,因此不存在递归。我认为这避免了无限递归的原因是它实际上根本就不会递归!仔细观察:
>>> isinstance(1, type)
False
>>> isinstance("", type)
False
>>> isinstance({}, type)
False
>>> isinstance((), type)
False
>>> type(object) == type
True
>>> isinstance(object, type)
True
>>> isinstance(object(), type)
False
>>> class a(): pass
...
>>> isinstance(a, type)
False
>>> isinstance(a(), type)
False
请注意,get\u info\u extractor
是一个方法,它调用了一个非方法函数,该函数恰好也被命名为get\u info\u extractor
,因此它不调用自身,因此不存在递归。我想,ies\u实例是一个dict,在某个点上做了类似于self的事情。它确实进入了无限递归…我猜,实例是一个dict,并且在某个点上做了类似self的事情。它确实进入了无限递归。。。