Dill.detect.children
需要两个参数:obj
和objtype
。
检查一个我可以调用的音频对象:
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)
的结果吗?
发布于 2014-09-03 16:45:31
types.MethodType 用户定义的类实例的方法类型。
>>> import types
>>> print types.MethodType
<type 'instancemethod'>
>>> p = Process()
>>> type(p.start) == types.MethodType
True
然而,我不认为dill.detect.children
会像你想的那样做。它的docstring说:
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
类型匹配的所有属性”是不一样的,这至少是您期望它所做的。
https://stackoverflow.com/questions/25648945
复制相似问题