[SciPy-user] matrix construction

Fernando Perez Fernando.Perez at colorado.edu
Tue Sep 28 13:47:58 EDT 2004


Robert Kern schrieb:
> Alan G Isaac wrote:
> 
>>On Fri, 24 Sep 2004, Fernando Perez apparently wrote:
>>
>>
>>>dims = 4
>>>size = 52
>>
>>
>>>a = N.zeros((size,)*dims)
>>>for line in open('data'):
>>>    a[map(int,line.split())] += 1
>>
>>
>>
>>This list seems to accept that nonprogrammers
>>are using SciPy, so hopefully my question
>>is not too OT:
>>
>>Is the fate of the implicit file descriptor
>>determinate (e.g., because it was not explicitly
>>bound)?
> 
> 
> IIRC, the file object has only one reference while the loop is running 
> since it was not assigned a name. Once the loop is over, the reference 
> count drops to 0 and the object ought to be garbage collected at which 
> time it ought to be closed. The latter is not guaranteed, however, so it 
> is recommended practice to assign the object to a name and explicitly 
> close the object after you are done with it. On the other hand, Python 
> usually works as I described it, so I do things like the example when 
> I'm in the interactive interpreter and take the extra precaution in 
> actual code.

This is totally correct.  In general, if I'm only reading data I even do 
things like the above in real code, since there is no problem with a dangling 
file descriptor (that I can think of).  But if you are _writing_ to a file, 
then you should defintely always call the close() method manually (which 
requires binding it to a name) instead of relying on it being closed when 
going out of scope.  Relying on such behavior can lead to data loss, since the 
closing is _not_ guaranteed to happen (I think jython and CPython differ in 
this respect, for example).

Cheers,

f




More information about the SciPy-User mailing list