Python 在类中调用时出现pickle错误

Python 在类中调用时出现pickle错误,python,pickle,Python,Pickle,我试图在我的一个类中放入一个方法,该方法允许我对文件进行pickle和unpickle。比如说,我有 import pickle class SomeClass: def otherMethods: pass def save_to_file(self, filename, file_to_save): with (filename,'wb') as output: pickle.dump(file_to_save,o

我试图在我的一个类中放入一个方法,该方法允许我对文件进行pickle和unpickle。比如说,我有

import pickle

class SomeClass:

    def otherMethods:
        pass

    def save_to_file(self, filename, file_to_save):
        with (filename,'wb') as output:
            pickle.dump(file_to_save,output,pickle.HIGHEST_PROTOCOL)
        print("Data has been saved.")
现在,当我创建这个“SomeClass”的实例时,我希望能够从终端调用如下内容

myfile = [1,2,3] # or anything else
SomeClass.save_to_file('myfile.pk',myfile)
但是,抛出的是:

'AttributeError: __exit__'

我曾在一些不同的帖子中看到有人在处理类似的用例时遇到困难,但我还没有弄清楚他们是如何在我的情况下应用的。非常感谢您的帮助。

open
缺失:

with open(filename,'wb') as output:

with语句要求上下文管理器使用
\uuuuuuuuuuuuuuuuuuu
\uuuuuuuuuuuuuuuu
方法,并引发
属性错误,因为元组
(文件名,'wb')
没有它们。

是否调用了
SomeClass.save\u-to\u-file
SomeClassObj.save\u-to\u-file
?我创建了SomeClass的实例。。。i、 e a=SomeClass(),然后a.save_to_file(…)可能会简化“with”statement@donfede如何简化“with”语句?