允许ref计数关闭文件项不好的样式? [英] Allowing ref counting to close file items bad style?

查看:51
本文介绍了允许ref计数关闭文件项不好的样式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是否气馁?:


for open in line(filename):

<做某事与行>


也就是说,我应该这样做吗?:


fileptr = open(文件名)

for file in fileptr:

<使用line>做某事

fileptr.close()

我可以指望引用计数为零以关闭文件?

写案例怎么样?例如:


类Foo(列表):

def __init __(自我):

self.extend([1, 2,3,4])

def写(self,fileptr):

for self in item:

fileptr.write(" %s \ n"%item)


foo_obj = Foo()

foo_obj.write(打开(。文件,w ;))


如果我明确关闭,我的数据会更安全吗?:

fileptr = open(" the.file"," w" ;)

foo_obj.write(fileptr)

fileptr.close()


我明白即将到来的''带' '声明将消除这个

的问题,但如果没有''带''怎么样?


/ Dan


-

dedded att verizon dott net

Is this discouraged?:

for line in open(filename):
<do something with line>

That is, should I do this instead?:

fileptr = open(filename)
for line in fileptr:
<do something with line>
fileptr.close()

Can I count on the ref count going to zero to close the file?

How about a write case? For example:

class Foo(list):
def __init__(self):
self.extend([1, 2, 3, 4])
def write(self, fileptr):
for item in self:
fileptr.write("%s\n" % item)

foo_obj = Foo()
foo_obj.write(open("the.file", "w"))

Is my data safer if I explicitly close, like this?:
fileptr = open("the.file", "w")
foo_obj.write(fileptr)
fileptr.close()

I understand that the upcoming ''with'' statement will obviate this
question, but how about without ''with''?

/Dan

--
dedded att verizon dott net

推荐答案

Dan< bo ***** @ foo.orgwrites :
Dan <bo*****@foo.orgwrites:

这是否气馁?:


for open in line(filename):

<使用line>做某事
Is this discouraged?:

for line in open(filename):
<do something with line>



是。

Yes.


我可以依靠引用计数为零来关闭文件吗?
Can I count on the ref count going to zero to close the file?



你真的不应该。它是CPython神器。

You really shouldn''t. It''s a CPython artifact.


我明白即将发布的''with''语句将消除这个

的问题,但怎么样没有''带''?
I understand that the upcoming ''with'' statement will obviate this
question, but how about without ''with''?



f = open(文件名)

试试:

for line in f:

<用行做>

终于:

f.close()

f = open(filename)
try:
for line in f:
<do something with line>
finally:
f.close()


Paul Rubin写道:
Paul Rubin wrote:

Dan< bo ***** @ foo.orgwrites:
Dan <bo*****@foo.orgwrites:

> Is这个气馁?:

对于开放行(文件名):
<用行做>
>Is this discouraged?:

for line in open(filename):
<do something with line>



是的。


Yes.



好​​吧,不是我想听的,而是我的预期。


谢谢,

Dan


-

dedded att verizon dott net

Well, not what I wanted to hear, but what I expected.

Thanks,
Dan

--
dedded att verizon dott net


Paul Rubin写道:
Paul Rubin wrote:

Dan< bo ***** @ foo.orgwrites:
Dan <bo*****@foo.orgwrites:

这是否气馁?:


for line in open(filename):

<用line>做某事
Is this discouraged?:

for line in open(filename):
<do something with line>



是的。


Yes.


我可以依靠引用计数为零来关闭文件吗?
Can I count on the ref count going to zero to close the file?



你真的不应该。这是一个CPython神器。


You really shouldn''t. It''s a CPython artifact.



我有点不同意。不,你不应指望参考计数。每个

se变为0.并且你不应该指望文件对象是GC''d
_immediately_在最后一个参考被销毁之后。不过,你应该能够在不太可能遥远的

未来的某个时刻依靠GC来实现。

做一个明确的.close()通常不是很有用,而且会使代码变得混乱

(并引入更多行以防止潜在的错误)。


是的,我知道语言规范在技术上允许没有GC

all - 这是QOI问题,不是规范问题,而是任何实现

没有GC作为一般的Python平台是无用的(对于特定的嵌入式用途可能有用

,但是对于这样的环境编程

将与编程不同对于合理的python平台来说,比这更好的方式比这更好了。


(而且我个人认为保证程序员的好处

重新计算语义将超过

Jython和其他替代实现的额外麻烦。

I disagree, somewhat. No, you shouldn''t count on the "ref count" per
se going to 0. And you shouldn''t count on the file object being GC''d
_immediately_ after the last reference is destroyed. You should be able
to rely on it being GC''d at some point in the not-horribly-distant
future, though.

Doing an explicit .close() is not normally useful and muddies the code
(and introduces more lines for potential bugs to infest).

And yes, I know that the language spec technically allows for no GC at
all--it''s a QOI issue, not a spec issue, but any implementation that
didn''t GC would be useless as a general Python platform (perhaps useful
for specific embedded uses, but programming for such an environment
would be different from programming for rational python platforms in
bigger ways than this).

(And personally I think the benefits to programmers of guaranteeing
ref-counting semantics would outweigh the additional headaches for
Jython and other alternative implementations).


这篇关于允许ref计数关闭文件项不好的样式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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