Re: [Tutor] how to set a value to a block of memory

2006-03-22 Thread Alan Gauld
 How can i set a value to a bytes of block of memory. In C, i think they 
 use
 memset like this.

Python is a high level programming language and does not support
direct memory access in the way that C does.

Can you explain why you think you need to do this?
Is it essential to access a specific memory location or do you
simply want to fill a data table with values? If the latter then
Python can help if you need direct memory access you will
need to use C I think..

Alan G
Author of the learn to program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to set a value to a block of memory

2006-03-21 Thread Kent Johnson
Keo Sophon wrote:
 Hi all,
 
 How can i set a value to a bytes of block of memory. In C, i think they use 
 memset like this.

Python does not support direct access to memory, you will need to use 
another language or maybe a C extension to Python to do this.

Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to set a value to a block of memory

2006-03-21 Thread Danny Yoo


On Tue, 21 Mar 2006, Kent Johnson wrote:

  How can i set a value to a bytes of block of memory. In C, i think
  they use memset like this.

Hi Sophon,

Secondary question: why are you trying to do this?  Are you trying to
represent a collection or array of things?


Some concepts in C aren't well represented in Python because they only
make sense from a low-level hardware perspective.  For example, asking for
an equivalent for C's malloc() or free() functions is nonsensical in the
sense that, since Python is garbage collected, it provides no such
functions to the casual Python user.

(Such functions ARE available through third-party modules such as SWIG,
but unless you are really trying to integrate with a C library, you don't
need this.)


As an extended example: C programmers often use malloc() to dynamically
build structures, such as linked lists:

/**/
/*** C Code **/
#include stdio.h
#include stdlib.h

struct IntList {
  int first;
  struct IntList *rest;
};

struct IntList* construct(int head, struct IntList* rest) {
  struct IntList* newlist;
  newlist = malloc(sizeof(struct IntList));
  newlist-first = head;
  newlist-rest = rest;
  return newlist;
}

void printList(struct IntList* list) {
  while (list != NULL) {
printf(%d\n, list-first);
list = list-rest;
  }
}

int main() {
  struct IntList *mylist = NULL;
  mylist = construct(5, mylist);
  mylist = construct(1, mylist);
  mylist = construct(4, mylist);
  mylist = construct(1, mylist);
  mylist = construct(3, mylist);
  printList(mylist);
  return 0;
}
/**/


But in Python, we can do this structure building without explicitely
malloc()ing.  The code above has a possible translation which looks
like:



## Python Code ##
import sys
class LinkedList:
def __init__(self, first, rest):
self.first, self.rest = first, rest

def printList(list):
while list != None:
print list.first
list = list.rest

def main():
mylist = None
mylist = LinkedList(5, mylist)
mylist = LinkedList(1, mylist)
mylist = LinkedList(4, mylist)
mylist = LinkedList(1, mylist)
mylist = LinkedList(3, mylist)
printList(mylist)
sys.exit(0)

if __name__ == '__main__':
main()


where most of the low-level details of allocating memory are all handled
by the Python runtime.

So rather than ask for direct equivalents to C, it might help to ask about
what you're trying to do.  We'll do what we can to help translate between
C concepts and Python concepts, and for the most part, what you already
know will have close analogues.  But some concepts will require a bit of
tweaking.


Good luck!


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to set a value to a block of memory

2006-03-21 Thread Danny Yoo


 On Tue, 21 Mar 2006, Kent Johnson wrote:
   
   How can i set a value to a bytes of block of memory. In C, i think
   they use memset like this.


Whoops, sorry about that Kent!  I completely messed up the attribution
when cutting-and-pasting.

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor