Dill.detect.children object types

孤街醉人 提交于 2019-12-25 05:15:23

问题


Dill.detect.children requires two arguments; obj and objtype.

Inspecting an audiofile object I can call:

dill.detect.children(audiofile, object)
dill.detect.children(audiofile, dict)
dill.detect.children(audiofile, list)

Which return without error.

But how about looking for instance methods?

type(audiofile.save)

returns

instancemethod

Tried

dill.detect.children(audiofile, instancemethod)

which returns

NameError: name 'instancemethod' is not defined

Tried

dill.detect.children(audiofile, 'instancemethod')

which returns

TypeError: isinstance() arg 2 must be a class, type, or tuple of classes and types

Shouldn't this return similar results to calling dir(audiofile)?


回答1:


Use types.MethodType:

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

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).

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.



来源:https://stackoverflow.com/questions/25648945/dill-detect-children-object-types

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!