Perplexing assignment behavior with h5py object as instance variable

后端 未结 2 1390
鱼传尺愫
鱼传尺愫 2021-01-14 03:30

I\'m using h5py to access HDF5 files and store the h5py File objects in a class. But I\'m experiencing some strange behavior in attempting to reassign a closed h5py file ins

2条回答
  •  暖寄归人
    2021-01-14 04:00

    Not sure if this will help, but searching through the source code I found this (abbreviated):

    class HLObject(object):
        def __nonzero__(self):
            register_thread()
            return self.id.__nonzero__()
    
    class Group(HLObject, _DictCompat):
        ...
    
    class File(Group):
        def __repr__(self):
            register_thread()
            if not self:
                return ""
            return '' % \
                (os.path.basename(self.filename), self.mode,
                 _extras.sizestring(self.fid.get_filesize()))
    

    Because there is no __str__ method, __repr__ is called to produce the output, and __repr__ first calls register_thread(), then checks to see if self is alive (better known as evaluating to True or False).

    Python then searches the classes until it finds __nonzero__ (which again calls register_thread()), then returns self.id.__nonzero__(), which is apparently returning False.

    So, you are correct in that the issue is not with the name binding (assignment), but why register_thread and/or self.id is bombing out on you, I do not know.

提交回复
热议问题