[Tutor] help with arrays

Alan Gauld alan.gauld at btinternet.com
Fri May 18 09:52:27 CEST 2007

"Stephen Adler" <adler at stephenadler.com> wrote

> I'm quite new to python and come from a c++/old school
> math/computing/physics background.

Thats OK, many of us came from there too.
Its not permanently damaging! :-)

> is that I can't for the life of me figure out how to allocate a 1meg
> buffer. (or any arbitrary sized buffer) without using something like 
> the
> range function.

First I have to ask why you need to? Thats an fairly
unusual requirement in Python where storage is nearly
always dynamically allocated.

> It would be nice to do something like
> a=string(100000)
> or something like that which would create a string 1000000 
> characters
> long. And then use that as input into the array module so that I can
> build up an array. What's the standard convention to do this?

And again why do you need to use an array, thats also fairly
uncommon. Can you not just allocate a dynamically created
string to a normal Python list?

Thee are valid cases in Python where you do need to
preallocate a block of storage and where the array module
is the best choice but before going there lets be sure we
need to!

> Also, how about pointers?

Most things in Python are effectively pointers in that all variables
in Python are effectively references in C++ terms. There are no
static variables. Everything is passed by reference.

> I'm using a c++ wrapped in python package
> called vtk and there are some function or class "attributes" which
> return pointers. How do I create a pointer reference?

Its just a name in Python

myName = returnMyPointer()


Alan Gauld
Author of the Learn to Program web site

More information about the Tutor mailing list