使用h5py对象作为实例变量而感到困惑 [英] Perplexing assignment behavior with h5py object as instance variable

查看:114
本文介绍了使用h5py对象作为实例变量而感到困惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用h5py访问HDF5文件并将h5py File对象存储在一个类中.但是在尝试使用新变量重新分配已关闭的h5py文件实例变量时,我遇到了一些奇怪的行为:

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 instance variable with a new one:

class MyClass:
    def __init__(self, filename):
        self.h5file = None
        self.filename = filename

    def vartest(self):
        self.h5file = h5py.File(self.filename, 'r')
        print self.h5file
        self.h5file.close()
        print self.h5file
        newh5file = h5py.File(self.filename, 'r')
        print newh5file
        self.h5file = newh5file
        print self.h5file
        print newh5file

def main():
    filename = sys.argv[1]
    mycls = MyClass(filename)
    mycls.vartest()

输出:

<HDF5 file "test.h5" (mode r, 92.7M)>
<Closed HDF5 file>
<HDF5 file "test.h5" (mode r, 92.7M)>
<Closed HDF5 file>
<Closed HDF5 file>

尝试用新打开的h5py File对象更新实例变量似乎在某种程度上影响了对象的状态,将其关闭.不管在h5py方面实现什么,从我对Python语言的理解(即,赋值运算符都不会重载)来看,我看不出这种行为是如何有意义的.

Attempting to update the instance variable with the newly opened h5py File object appears to have somehow affected the state of the object, closing it. Regardless of the implementation on the h5py side, I don't see how this behavior makes sense from my understanding of the Python language (i.e., no overloading of the assignment operator).

此示例在Python 2.6.5和h5py 1.3.0上运行.如果您想尝试此示例,但是没有HDF5文件,则可以将文件访问模式从"r"更改为"a".

This example is run with Python 2.6.5 and h5py 1.3.0. If you want to try this example but don't have an HDF5 file sitting around you can just change the file access mode from 'r' to 'a'.

推荐答案

是的,这是h5py 1.3中的一个已知错误,当​​您使用HDF5 1.8.5或更高版本时会显示该错误.它与1.8.5中处理标识符的方式有关.您可以通过使用HDF5 1.8.4或更早版本或升级到h5py 2.0来对其进行修复.

Yes, this is a known bug in h5py 1.3, which shows up when you use HDF5 1.8.5 or newer. It's related to changes in the way identifiers are handled in 1.8.5. You can fix it by using HDF5 1.8.4 or earlier, or by upgrading to h5py 2.0.

这篇关于使用h5py对象作为实例变量而感到困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆