Python C API Question.

A

Adonis

This is my first crack at using Python from a C level, and also a long time
since I have even used C, so my wording might be off, when creating a new
list utilizing the PyList_New(...) function, instead of creating a list of a
fixed length, is there a way to create a variabled length instead?

Any help is greatly apprecited.

Adonis
 
D

Diez B. Roggisch

Adonis said:
This is my first crack at using Python from a C level, and also a long
time since I have even used C, so my wording might be off, when creating a
new list utilizing the PyList_New(...) function, instead of creating a
list of a fixed length, is there a way to create a variabled length
instead?

AFAIK they are always variable length - they always support appending to
them. The argument is just about the initial capacity, so if you know that
beforehand, you can optimize allocation of list entries.
 
J

Jeff Epler

Please see
http://docs.python.org/api/listObjects.html

C's PyList_Append() is roughly equivalent to Python's list.append().
If you want to construct a list without knowing how many items will
eventually be in it, use PyList_New(0), then PyList_Append for each
item you want to add. This will be as efficient as the similar
Python code
l = [f(x) for x in s]
in terms of the number of resizes the internal array-of-pointers
undergoes.

Jeff

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.6 (GNU/Linux)

iD8DBQFBenQpJd01MZaTXX0RAqfMAKCUzOhSLjNezjDW71GQwQdWWgmgoQCgq3lT
27if6lFdJCQ6yLp8b6/UYQg=
=XAeP
-----END PGP SIGNATURE-----
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,209
Messages
2,571,088
Members
47,687
Latest member
IngridXxj

Latest Threads

Top