Re: Many newbie questions regarding python

2010-10-11 Thread Peter Pearson
On Sun, 10 Oct 2010 18:36:27 -0700, Ethan Furman wrote: [snip] > > My question is more along the lines of: a mutable object was passed in > to func()... what style of loop could be used to turn that one object > into /n/ distinct objects? A list comp won't do it, but neither will a > for loop,

Re: Many newbie questions regarding python

2010-10-11 Thread Jean-Michel Pichavant
Peter Pearson wrote: On Sat, 09 Oct 2010 19:30:16 -0700, Ethan Furman wrote: Steven D'Aprano wrote: [snip] But that doesn't mean that the list comp is the general purpose solution. Consider the obvious use of the idiom: def func(arg, count): # Initialise the list. L = [ar

Re: Many newbie questions regarding python

2010-10-10 Thread Ethan Furman
Peter Pearson wrote: On Sat, 09 Oct 2010 19:30:16 -0700, Ethan Furman wrote: Steven D'Aprano wrote: [snip] But that doesn't mean that the list comp is the general purpose solution. Consider the obvious use of the idiom: def func(arg, count): # Initialise the list. L = [arg for i in

Re: Many newbie questions regarding python

2010-10-10 Thread Peter Pearson
On Sat, 09 Oct 2010 19:30:16 -0700, Ethan Furman wrote: > Steven D'Aprano wrote: [snip] >> But that doesn't mean that the list comp is the general purpose solution. >> Consider the obvious use of the idiom: >> >> def func(arg, count): >> # Initialise the list. >> L = [arg for i in range(

Re: Many newbie questions regarding python

2010-10-09 Thread Ethan Furman
Steven D'Aprano wrote: And how often do you have an list that you are creating where you don't know what items you have to initialise the list with? [snip] You are right to point out that the third case is a Python gotcha: [[]]*n doesn't behave as expected by the naive or inexperienced Python

Re: Many newbie questions regarding python

2010-10-09 Thread Hrvoje Niksic
alex23 writes: > If anything, I feel like the list comp version is the correct solution > because of its reliability, whereas the multiplication form feels like > either a lucky naive approach or relies on the reader to know the type > of the initialising value and its mutability. Other than lis

Re: Many newbie questions regarding python

2010-10-08 Thread Steven D'Aprano
On Thu, 07 Oct 2010 18:34:58 -0700, alex23 wrote: > On Oct 8, 10:27 am, Steven D'Aprano cybersource.com.au> wrote: >> >     v = [0 for i in range(20)] >> >> Absolutely not. Such a code snippet is very common, in fact I've done >> it myself, but it is a "hammer solution" -- to a small boy with a >

Re: Many newbie questions regarding python

2010-10-08 Thread Carl Banks
On Oct 7, 4:10 pm, Rogério Brito wrote: [snip] > >     v = [0 for i in range(20)] > >     v = [0] * 20 > >     v = [] >     for i in range(20): v.append(0) > > What should I prefer? Any other alternative? The Pythonic way is to not to preinitialize the list at all. Don't put anything in the list

Re: Many newbie questions regarding python

2010-10-08 Thread Andreas Waldenburger
On Thu, 7 Oct 2010 18:34:58 -0700 (PDT) alex23 wrote: > On Oct 8, 10:27 am, Steven D'Aprano cybersource.com.au> wrote: > > >     v = [0 for i in range(20)] > > > > Absolutely not. Such a code snippet is very common, in fact I've > > done it myself, but it is a "hammer solution" -- to a small boy

Re: Many newbie questions regarding python

2010-10-08 Thread nn
On Oct 7, 7:10 pm, Rogério Brito wrote: > Hi there. > > I am used to some languages like C, but I am just a complete newbie with > Python > and, while writing some small snippets, I had encountered some problems, with > which I would sincerely appreciate any help, since I appreciate this language

Re: Many newbie questions regarding python

2010-10-08 Thread alex23
On Oct 8, 10:27 am, Steven D'Aprano wrote: > >     v = [0 for i in range(20)] > > Absolutely not. Such a code snippet is very common, in fact I've done it > myself, but it is a "hammer solution" -- to a small boy with a hammer, > everything looks like a nail that needs hammering. Writing such a li

Re: Many newbie questions regarding python

2010-10-08 Thread gregf...@gmail.com
On Oct 7, 6:10 pm, Rogério Brito wrote: > Hi there. > > I am used to some languages like C, but I am just a complete newbie with > Python > and, while writing some small snippets, I had encountered some problems, with > which I would sincerely appreciate any help, since I appreciate this language

Re: Many newbie questions regarding python

2010-10-08 Thread Emile van Sebille
On 10/8/2010 10:15 AM Grant Edwards said... Damn. I should give up and go golfing. +1 QOTW Emile -- http://mail.python.org/mailman/listinfo/python-list

Re: Many newbie questions regarding python

2010-10-08 Thread Grant Edwards
On 2010-10-08, Grant Edwards wrote: > On 2010-10-08, Grant Edwards wrote: >> On 2010-10-07, Rog??rio Brito wrote: >> >>> If possible, I would like to simply declare the list and fill it >>> latter in my program, as lazily as possible (this happens notoriously >>> when one is using a technique of

Re: Many newbie questions regarding python

2010-10-08 Thread Grant Edwards
On 2010-10-08, Grant Edwards wrote: > On 2010-10-07, Rog??rio Brito wrote: > >> If possible, I would like to simply declare the list and fill it >> latter in my program, as lazily as possible (this happens notoriously >> when one is using a technique of programming called dynamic >> programming w

Re: Many newbie questions regarding python

2010-10-08 Thread Grant Edwards
On 2010-10-07, Rog??rio Brito wrote: > If possible, I would like to simply declare the list and fill it > latter in my program, as lazily as possible (this happens notoriously > when one is using a technique of programming called dynamic > programming where initializing all positions of a table m

Re: Many newbie questions regarding python

2010-10-08 Thread Tim Harig
On 2010-10-08, BartC wrote: > "Rogério Brito" wrote in message > news:i8lk0n$g3...@speranza.aioe.org... >> If possible, I would like to simply declare the list and fill it latter in >> my >> program, as lazily as possible (this happens notoriously when one is using >> a >> technique of program

Re: Many newbie questions regarding python

2010-10-08 Thread BartC
"Rogério Brito" wrote in message news:i8lk0n$g3...@speranza.aioe.org... My first try to write it in Python was something like this: v = [] for i in range(20): v[i] = 0 Unfortunately, this doesn't work, as I get an index out of bounds when trying to index the v list. Python can't grow

Re: Many newbie questions regarding python

2010-10-08 Thread Jean-Michel Pichavant
Rogério Brito wrote: class C: f = 1 def g(self): return f I get an annoying message when I try to call the g method in an object of type C, telling me that there's no global symbol called f. If I make g return self.f instead, things work as expected, but the code loses some reada

Re: Many newbie questions regarding python

2010-10-07 Thread Steven D'Aprano
On Thu, 07 Oct 2010 20:10:14 -0300, Rogério Brito wrote: > What is the Pythonic way of writing code like this? So far, I have > found many alternatives and I would like to write code that others in > the Python community would find natural to read. Some of the things > that crossed my mind: > >

Re: Many newbie questions regarding python

2010-10-07 Thread Tim Harig
On 2010-10-07, Rogério Brito wrote: > 1 - The first issue that I am having is that I don't seem to be able to, say, > use something that would be common for people writing programs in C: defining > a > one-dimensional vector and only initializing it when needed. > > For instance, in C, I would wr

Re: Many newbie questions regarding python

2010-10-07 Thread Andreas Waldenburger
On Fri, 08 Oct 2010 00:46:41 +0100 MRAB wrote: > In other words, don't try to write a C program in Python! Man, I'm good. :D /W -- To reach me via email, replace INVALID with the country code of my home country. But if you spam me, I'll be one sour kraut. -- http://mail.python.org/mailman

Re: Many newbie questions regarding python

2010-10-07 Thread Andreas Waldenburger
On Thu, 07 Oct 2010 20:10:14 -0300 Rogério Brito wrote: > I am used to some languages like C, but I am just a complete newbie > with Python and, while writing some small snippets, I had encountered > some problems, with which I would sincerely appreciate any help, > since I appreciate this langua

Re: Many newbie questions regarding python

2010-10-07 Thread MRAB
On 08/10/2010 00:10, Rogério Brito wrote: Hi there. I am used to some languages like C, but I am just a complete newbie with Python and, while writing some small snippets, I had encountered some problems, with which I would sincerely appreciate any help, since I appreciate this language to write