Re: file pointer array



FSH <lee.joosang@xxxxxxxxx> writes:

I have a simple question. I wish to generate an array of file
pointers.

Welcome to Python.

By your description, I think you want a different type: not an array,
but a list.

I recommend you become familiar with Python's data model
<URL:http://docs.python.org/reference/datamodel.html>.

I wish to generate fine pointer array so that I can read the files at
the same time.

I don't know about reading the files *at the same time*.

If you mean you want to have multiple files open simultaneously and read
from any of them arbitrarily, you don't need any particular container
type; you just need to have references to those open file objects.

Any references will do for that; you don't need pointers in Python.

for index in range(N):
fid[index] = open('data%d.txt' % index,'r')

Python has iterable types built in, so you rarely need to maintain an
index yourself.

Further, Python has “list comprehension”, allowing you to define a list
in a single statement by describing how each element is created::

fid = [open('data%d.txt' % count, 'r') for count in range(N)]

To learn about this and other useful topics, you should work through
Python's tutorial <URL:http://docs.python.org/tutorial/> from start to
finish, doing each exercise until you understand the concept being
explained.

--
\ “We have to go forth and crush every world view that doesn't |
`\ believe in tolerance and free speech.” —David Brin |
_o__) |
Ben Finney
.



Relevant Pages

  • Re: Modifying Class Object
    ... > Python as a language is implemented in many languages. ... > This set of operational semantics is written in Haskell. ... > therefore has no pointers or references, ...
    (comp.lang.python)
  • Re: Modifying Class Object
    ... > Python as a language is implemented in many languages. ... > This set of operational semantics is written in Haskell. ... > therefore has no pointers or references, ...
    (comp.lang.python)
  • Re: list implementation
    ... >I believe the type "list" is implemented as an array of pointers. ... A Python list is sematically/behaviorally defined as a mutable extensible ... the references are arrays of C pointers. ...
    (comp.lang.python)
  • Re: How to Teach Python "Variables"
    ... "variables" in the sense that, say, C does, and instead has bindings ... a redefinition of terms programmers are already familiar with. ... Python "variables" do not work the way they do in ... of pointers in C is implementing pass-by-reference. ...
    (comp.lang.python)
  • Re: why cannot assign to function call
    ... as in Python, Java, REALbasic, .NET, etc. ... Pointers are passed and assigned by value, ... If you simply use the name, you get by-value semantics. ... Python's assignment semantics, and further appear ...
    (comp.lang.python)