Re: ctypes - python2.7.3 vs python3.2.3
On 8/28/12 23:51 , John Gordon wrote: In <18eb8025-7545-4d10-9e76-2e41deaad...@googlegroups.com> Rolf writes: uint32_t myfunction (char ** _mydata) { char mydata[16]; strcpy(mydata, "Hello Dude!"); *_mydata = mydata; return 0; } mydata is an auto variable, which goes out of scope when myfunction() exits. *_mydata ends up pointing to garbage. I'm not completely sure, but i think this can be solved by using: static char mydata[16]; (Btw.: I don't know why you use char ** _mydata, i would use char * _mydata, but then again, i'm not very familiar with ctypes) Jan Kuiken -- http://mail.python.org/mailman/listinfo/python-list
Re: Objects in Python
On 8/23/12 20:17 , Ian Kelly wrote: ... Well, there you go. There *is* something wrong with having six variables called 'q'. Sometimes you don't want only six variables called 'q' but a hundred of them :-) def fac(q): if q < 1 : return 1 else: return q * fac(q-1) print(fac(100)) That's only one variable called 'q', instantiated 100 times simultaneously. Bare with me, i come from a C world, and think of each variable, whatever its name or scope, as a piece of memory and therefore different. btw. I like the idea of simultaneously instantiation :-) Jan Kuiken -- http://mail.python.org/mailman/listinfo/python-list
Re: Objects in Python
On 8/23/12 06:11 , Steven D'Aprano wrote: 2) Related to the above, you can infinitely nest scopes. There's nothing wrong with having six variables called 'q'; you always use the innermost one. Yes, this can hurt readability Well, there you go. There *is* something wrong with having six variables called 'q'. Sometimes you don't want only six variables called 'q' but a hundred of them :-) def fac(q): if q < 1 : return 1 else: return q * fac(q-1) print(fac(100)) Jan Kuiken -- http://mail.python.org/mailman/listinfo/python-list
Re: Help doing it the "python way"
On 5/24/12 22:22 , Scott Siegler wrote: I am an experienced programmer but a beginner to python. As such, I can figure out a way to code most algorithms using more "C" style syntax. I am doing something now that I am sure is a more python way but i can't quite get it right. I was hoping someone might help. So I have a list of grid coordinates (x, y). From that list, I want to create a new list that for each coordinate, I add the coordinate just above and just below (x,y+1) and (x,y-1) right now I am using a for loop to go through all the coordinates and then separate append statements to add the top and bottom. is there a way to do something like: [(x,y-1), (x,y+1) for zzz in coord_list] or something along those lines? If you have lot's of numerical data you can use the NumPy module (http://numpy.scipy.org/), your problem would reduce to something like this (copied from an IPython shell, could be shorter) Regards, Jan Kuiken In [1]: first_list = np.arange(0, 10).reshape((5,2)) In [2]: above = np.array([0,-1]) In [3]: below = np.array([0,+1]) In [4]: N,d = first_list.shape In [5]: second_list = np.empty((N*2,d)) In [6]: second_list[0::2] = first_list + above In [7]: second_list[1::2] = first_list + below In [8]: second_list Out[8]: array([[ 0., 0.], [ 0., 2.], [ 2., 2.], [ 2., 4.], [ 4., 4.], [ 4., 6.], [ 6., 6.], [ 6., 8.], [ 8., 8.], [ 8., 10.]]) In [9]: first_list Out[9]: array([[0, 1], [2, 3], [4, 5], [6, 7], [8, 9]]) -- http://mail.python.org/mailman/listinfo/python-list
Re: functions which take functions
On 4/9/12 20:57 , Kiuhnm wrote: Do you have some real or realistic (but easy and self-contained) examples when you had to define a (multi-statement) function and pass it to another function? I don't use it daily but the first argument of list.sort, i.e. the compare function springs to mind. Jan Kuiken -- http://mail.python.org/mailman/listinfo/python-list