[Python-ideas] pop multiple elements of a list at once

Diego Jacobi jacobidiego at gmail.com
Mon Jul 12 17:13:34 CEST 2010


Hi.
I apologize if i am having difficulties to explain myself in English.
Also i just realise that i wasnt answering to the list, but directly
to Brett Cannon. Sorry for that.

Also i am taking into account that you are right and my concept of how
memory is handled here for lists is way too different as how i am more
used to work in firmwares.

Anyway, the concept of my idea comes of my understanding of an
low-leveled array.

I do understand that poping returns only a pointer to that element.
I didnt understand that every element inside a list is also a pointer
to a python type, so the data is not copied, but the pointer is.
The thing is that the elements on my list are just bytes. (unsigned
char), and i think that an array of this type of data is called in
python immutable, which means that i may not be using the right
datatype.

Anyway. slice support on pop(), and/or the ability to skip elements in
a for loop without restarting the iteration will clear up a lot of
code.



If my scenario is yet no clear i give below some more details:

When i think on how this will work on memory:
def pop_slice(lis, n):
 tem = lis[:-n]
 del lis[:-n]
 return tem

I think in "copying the elements of an array":

BYTE* pop_slice(BYTE* list, unsigned int n){
    BYTE* temp = malloc(n*sizeof(BYTE));
    int i;
    for(i=0 ; i < n ; i++) {
       temp[i] = list[i];
    }
   free(list, n);
   return temp;
}


Most python books and tutorials clearly says that this operation
L[start:end]    copies the elements requested. And being copy i
understand the above behavior, which is less efficient than advancing
the pointer.



But i wanted to do (with "pop multiple bytes at once") is:


typedef unsigned char BYTE;

BYTE array[BIG_SIZE];
BYTE* incomming_buffer_pointer = &array[0];
BYTE* incomming_packet_pointer = &array[0];

BYTE* pop_slice(BYTE* list, unsigned int n){
    BYTE* temp;
    temp = list;
    list = &list[n];
    return temp;
}
..
incomming_packet_pointer = pop_slice( incomming_buffer_pointer  , PACKET_SIZE)
if (parse_packet_is_not_corrupt( incomming_packet_pointer ) )
     parse_new_packet( incomming_packet_pointer );
else
   ....
..



Thanks for analizing the idea.
Jacobi Diego



More information about the Python-ideas mailing list