首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Dill.detect.children对象类型

Dill.detect.children对象类型
EN

Stack Overflow用户
提问于 2014-09-03 16:06:40
回答 1查看 92关注 0票数 0

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

检查一个我可以调用的音频对象:

代码语言:javascript
运行
复制
dill.detect.children(audiofile, object)
dill.detect.children(audiofile, dict)
dill.detect.children(audiofile, list)

没有错误的返回。

但是如何寻找例如的方法呢?

代码语言:javascript
运行
复制
type(audiofile.save)

返回

代码语言:javascript
运行
复制
instancemethod

试过

代码语言:javascript
运行
复制
dill.detect.children(audiofile, instancemethod)

回传

代码语言:javascript
运行
复制
NameError: name 'instancemethod' is not defined

试过

代码语言:javascript
运行
复制
dill.detect.children(audiofile, 'instancemethod')

回传

代码语言:javascript
运行
复制
TypeError: isinstance() arg 2 must be a class, type, or tuple of classes and types

这不应该返回类似于调用dir(audiofile)的结果吗?

EN

回答 1

Stack Overflow用户

发布于 2014-09-03 16:45:31

使用types.MethodType

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

代码语言:javascript
运行
复制
>>> import types
>>> print types.MethodType
<type 'instancemethod'>
>>> p = Process()
>>> type(p.start) == types.MethodType
True

然而,我不认为dill.detect.children会像你想的那样做。它的docstring说:

代码语言:javascript
运行
复制
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类型匹配的所有属性”是不一样的,这至少是您期望它所做的。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25648945

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档