Two candies

bearophileHUGS at lycos.com bearophileHUGS at lycos.com
Wed Jan 2 13:39:19 CET 2008


Two little things I may like added.

1) A fast and memory efficient way to create an array.array at a
certain size.
At the moment I can see some ways to do it:

from array import array
from itertools import repeat
a = array("l", xrange(n)) #1
a = array("l", [0]*n)) #2
a = array("l", repeat(0, n)) #3

- #1 doesn't initialize it to a constant, and to me it seems not
memory efficient.
- #2 is faster, but I think it requires twice the memory (so it's not
good when a has to be quite large).
- #3 interacts badly with Psyco (Psyco doesn't digest itertools at
all), and it seems not memory efficient.

So a simple syntax can be added, like a method a.allocate(item, n),
that takes an item and the length of the array to create:

a = array("l")
a.allocate(0, n)

(Note: I know about external numerical packages).
In alternative Psyco may be improved a bit, so the #3 syntax may
become the standard (efficient in memory and space) one.

---------------------------

2) When I use MatchObjects I have to look at the docs to remember the
difference between group() and groups() etc. So I suggest to add a
__getitem__ method to MatchObject, so this:

mo[3]

Equals to:

mo.group(3)

Bye,
bearophile



More information about the Python-list mailing list