initilize a memory zone in python

Diez B. Roggisch deets at nospam.web.de
Mon Aug 31 04:08:39 EDT 2009


Mug schrieb:
> On Aug 30, 8:58 pm, "Diez B. Roggisch" <de... at nospam.web.de> wrote:
>> Mug schrieb:
>>
>>> hello, i'm new in python, i used to program in C,
>>> i have a small problem, i tryed to do some serial port things
>>> manipulation
>>> with python.
>>> i have something like:
>>> import sys,termios
>>> fd = sys.stdin.fileno()
>>> term_conf=termios.tcgetattr(fd);
>>> now i want to modify the actuall values in term_conf  zone to zero
>>> i don't see how to do it,
>>> in C we can do : bzero(&term_conf,sizeof(struct termios));
>>> i want to know if it exist a similar function in python, thanks
>> In python you don't modify memory like that.
>>
>> For the above function, you pass a value as the one you got to
>> tcgetattr, with values modified as you desire them.
> i tryed
> print term_conf
> and i saw that the term_conf is actually represent with an array in
> python:
> 
> zsh/3 4201 [1] % python test.py
> [27906, 5, 1215, 35387, 15, 15, ['\x03', '\x1c', '\x7f', '\x15',
> '\x04', '\x00', '\x01', '\xff', '\x11', '\x13', '\x1a', '\xff',
> '\x12', '\x0f', '\x17', '\x16', '\xff', '\x00', '\x00', '\x00',
> '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00',
> '\x00', '\x00', '\x00', '\x00']]
> 
> it's a array of 7 elements, with the seventh element it self a array,
> so in order to initialize them to zero, there's no other way than
> modify them
> one by one in a loop? is there a fonction like "memset" or "bzero" in
> python?

Again: no, there is no such function. But for the above, you'd easily write:

foo = [0] * 7 # only do this with immutables
foo[-1] = ['\x00'] * 16

alternatively, you modify the result you got from tcgetattr in the 
places you want.



Diez



More information about the Python-list mailing list