0

Dill.detect.children需要两个参数;objobjtype

检查我可以调用的音频文件对象:

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)吗?

4

1 回答 1

1

使用types.MethodType

类型.MethodType

用户定义的类实例的方法类型。

>>> import types
>>> print types.MethodType
<type 'instancemethod'>
>>> p = Process()
>>> type(p.start) == types.MethodType
True

但是,我认为dill.detect.children不会像您认为的那样。它的文档字符串说:

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”不同,它至少看起来是您期望它执行的操作。

于 2014-09-03T16:45:31.997 回答