问题描述
Dill.detect.children
需要两个参数; obj
和objtype
.
Dill.detect.children
requires two arguments; obj
and objtype
.
检查我可以调用的 audiofile 对象:
Inspecting an audiofile object I can call:
dill.detect.children(audiofile, object)
dill.detect.children(audiofile, dict)
dill.detect.children(audiofile, list)
返回没有错误.
但是寻找实例方法呢?
type(audiofile.save)
返回
instancemethod
尝试
dill.detect.children(audiofile, instancemethod)
返回
NameError: name 'instancemethod' is not defined
尝试
dill.detect.children(audiofile, 'instancemethod')
返回
TypeError: isinstance() arg 2 must be a class, type, or tuple of classes and types
这不应该返回与调用dir(audiofile)
类似的结果吗?
Shouldn't this return similar results to calling dir(audiofile)
?
推荐答案
使用 types.MethodType
:
用户定义的类实例的方法类型.
The type of methods of user-defined class instances.
>>> import types
>>> print types.MethodType
<type 'instancemethod'>
>>> p = Process()
>>> type(p.start) == types.MethodType
True
但是,我认为dill.detect.children
并没有您认为的那样.它的文档字符串说:
However, I don't think dill.detect.children
does what you think it does. Its docstring says:
children(obj, objtype, depth=1, ignore=())
Find the chain of referrers for obj. Chain will start with obj.
objtype: an object type or tuple of types to search for
depth: search depth (e.g. depth=2 is 'grandparents')
ignore: an object or tuple of objects to ignore in the search
NOTE: a common thing to ignore is all globals, 'ignore=globals()'
NOTE: repeated calls may yield different results, as python stores
the last value in the special variable '_'; thus, it is often good
to execute something to replace '_' (e.g. >>> 1+1).
这与在obj
上找到与类型objtype
匹配的所有属性"不同,它至少出现是您期望它执行的操作.
This is not the same as "find all attributes on obj
that match type objtype
", which it at least appears is what you're expecting it to do.
这篇关于Dill.detect.children对象类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!