[SciPy-user] matrix construction

Robert Kern rkern at ucsd.edu
Tue Sep 28 12:54:03 EDT 2004


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 example was quite useful, btw.
> 
> Thank you,
> Alan Isaac

-- 
Robert Kern
rkern at ucsd.edu

"In the fields of hell where the grass grows high
  Are the graves of dreams allowed to die."
   -- Richard Harter




More information about the SciPy-User mailing list