In python at runtime determine if an object is a class (old and new type) instance

前端 未结 3 1226
南旧
南旧 2021-01-13 10:01

I am trying to write a deeply nested set of classes, attributes, bound methods, etc. to a HDF5 file using the h5py module for long-term storage. I am really close. The onl

3条回答
  •  日久生厌
    2021-01-13 10:37

    Thanks to @user4815162342, I have been able to get this to work. Here is a slightly modified version that will return True for instances of old- and new-style classes:

    #Added the check for old-style class
    Py_TPFLAGS_HEAPTYPE = (1L<<9)       # Include/object.h
    def is_class_instance(o):
        import types
        return (bool(type(o).__flags__ & Py_TPFLAGS_HEAPTYPE) 
                or type(o) is types.InstanceType)
    

提交回复
热议问题