Why does inherting from object in Python, change they type of the __dict__ when not specifying a parent does not?
本问题已经有最佳答案,请猛点这里访问。
我有一段简单的代码,它试图为Python中的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | class File: def __init__(this, *args): this._file = file(*args) def __del__(this): this._file.close() def createCallForwarder(method): return lambda obj,*args: method(obj._file, *args) _dict = file.__dict__ for (k,v) in zip(_dict.keys(), _dict.values()): if not (k.startswith('__') and k.endswith('__')): if v.__class__.__name__ == 'method_descriptor': File.__dict__[k] = createCallForwarder(v) # get the repr method File.__repr__ = createCallForwarder(dict_proxy['__repr__']) |
如果我将
为什么不同?
您根本不应该直接访问
使用
1 2 3 4 5 6 | class File(object): def __init__(self, *args): self._file = open(*args) def __getattr__(self, name): return getattr(self._file, name) |
我还将代码转换为最佳实践:使用
对于新样式的对象(从
1 2 3 4 5 | class File(object): def __init__(self, *args): self._file = open(*args) for name in dir(self._file): setattr(self, name, getattr(self._file, name)) |
如果您所需要的只是一个自动关闭垃圾收集的文件对象,那么您会遇到很多麻烦。python文件对象已经有了一个这样做的
然而,最佳实践是使用文件对象作为上下文管理器,使用
1 2 3 4 | with open(somefilename) as fileobj: # do all sorts with fileobj # here, fileobj will have been closed automatically. |
引用
As of Python 2.5, you can avoid having to call this method explicitly if you use the with statement. For example, the following code will automatically close f when the
with block is exited:
1
2
3
4
5 from __future__ import with_statement # This isn't required in Python 2.6
with open("hello.txt") as f:
for line in f:
print line,
I was just trying to get a File object that closes itself
使用
1 2 3 4 5 6 | with open('somefile.txt') as the_file: for line in the_file: # do something with line # Once outside the with block, the file is automatically closed print('somefile.txt is closed here') |