Re: Scope of variable inside list comprehensions?

2011-12-06 Thread Jean-Michel Pichavant
Steven D'Aprano wrote: On Mon, 05 Dec 2011 19:57:15 +0100, Jean-Michel Pichavant wrote: The proper way to propagate information with exceptions is using the exception itself: try: songs = [Song(_id) for _id in song_ids] except Song.DoesNotExist, exc: print exc I'm not entire

Re: Scope of variable inside list comprehensions?

2011-12-06 Thread 88888 Dihedral
On Tuesday, December 6, 2011 2:42:35 PM UTC+8, Rainer Grimm wrote: > Hello, > > > try: > > songs = [Song(id) for id in song_ids] > > except Song.DoesNotExist: > > print "unknown song id (%d)" % id > that's is a bad programming style. So it will be forbidden with python 3. T

Re: Scope of variable inside list comprehensions?

2011-12-05 Thread Rainer Grimm
Hello, > try: > songs = [Song(id) for id in song_ids] > except Song.DoesNotExist: > print "unknown song id (%d)" % id that's is a bad programming style. So it will be forbidden with python 3. The reason is that list comprehension is a construct from the functional world.

Re: Scope of variable inside list comprehensions?

2011-12-05 Thread Chris Angelico
On Tue, Dec 6, 2011 at 9:57 AM, Roy Smith wrote: > I may be in the minority here, but it doesn't bother me much that my use of > 'id' shadows a built-in.  Especially in small scopes like this, I use > whatever variable names make the the code easiest to read and don't worry > about shadowing bu

Re: Scope of variable inside list comprehensions?

2011-12-05 Thread Terry Reedy
On 12/5/2011 5:36 PM, Roy Smith wrote: Well, in my defense, I did ask a pretty narrow question, "Is id guaranteed to be in scope in the print statement?". Yes for 2.x, guaranteed no for 3.x. If you had simply asked "Is the loop variable of a list comprehension guaranteed to be in scope after

Re: Scope of variable inside list comprehensions?

2011-12-05 Thread Roy Smith
Sigh. I attempted to reduce this to a minimal example to focus the discussion on the question of list comprehension variable scope. Instead I seem to have gotten people off on other tangents. I suppose I should post more of the real code... song_ids = request.POST.getlist('song_id')

Re: Scope of variable inside list comprehensions?

2011-12-05 Thread Steven D'Aprano
On Mon, 05 Dec 2011 19:57:15 +0100, Jean-Michel Pichavant wrote: > The proper way to propagate information with exceptions is using the > exception itself: > > try: > songs = [Song(_id) for _id in song_ids] > except Song.DoesNotExist, exc: > print exc I'm not entirely sure that this is

Re: Scope of variable inside list comprehensions?

2011-12-05 Thread Roy Smith
Well, in my defense, I did ask a pretty narrow question, "Is id guaranteed to be in scope in the print statement?". While I will admit that not knowing whether I could alter the exception, or whether id masked a builtin or not does complexify answering some questions, those are questions I didn

Re: Scope of variable inside list comprehensions?

2011-12-05 Thread Terry Reedy
On 12/5/2011 2:15 PM, Roy Smith wrote: Hmmm, the use of id was just a simplification for the sake of posting. The real code is a bit more complicated and used a different variable name, but that's a good point. As far as storing the value in the exception, unfortunately, DoesNotExist is not my

Re: Scope of variable inside list comprehensions?

2011-12-05 Thread Roy Smith
Hmmm, the use of id was just a simplification for the sake of posting. The real code is a bit more complicated and used a different variable name, but that's a good point. As far as storing the value in the exception, unfortunately, DoesNotExist is not my exception; it comes from deep within d

Re: Scope of variable inside list comprehensions?

2011-12-05 Thread Jean-Michel Pichavant
Roy Smith wrote: Consider the following django snippet. Song(id) raises DoesNotExist if the id is unknown. try: songs = [Song(id) for id in song_ids] except Song.DoesNotExist: print "unknown song id (%d)" % id Is id guaranteed to be in scope in the print statement? I

Re: Scope of variable inside list comprehensions?

2011-12-05 Thread Peter Otten
Roy Smith wrote: > Consider the following django snippet. Song(id) raises DoesNotExist if > the id is unknown. > > try: > songs = [Song(id) for id in song_ids] > except Song.DoesNotExist: > print "unknown song id (%d)" % id > > Is id guaranteed to be in scope in the prin

Re: Scope of variable inside list comprehensions?

2011-12-05 Thread Jussi Piitulainen
Roy Smith writes: > Consider the following django snippet. Song(id) raises DoesNotExist > if the id is unknown. > > try: > songs = [Song(id) for id in song_ids] > except Song.DoesNotExist: > print "unknown song id (%d)" % id > > Is id guaranteed to be in scope in the pri

Re: Scope of variable inside list comprehensions?

2011-12-05 Thread Chris Kaynor
On Mon, Dec 5, 2011 at 9:04 AM, Roy Smith wrote: > Consider the following django snippet. Song(id) raises DoesNotExist if > the id is unknown. > >try: >songs = [Song(id) for id in song_ids] >except Song.DoesNotExist: >print "unknown song id (%d)" % id > > Is id guaranteed

Scope of variable inside list comprehensions?

2011-12-05 Thread Roy Smith
Consider the following django snippet. Song(id) raises DoesNotExist if the id is unknown. try: songs = [Song(id) for id in song_ids] except Song.DoesNotExist: print "unknown song id (%d)" % id Is id guaranteed to be in scope in the print statement? I found one thread (

Re: scope of variable

2010-08-20 Thread John Nagle
On 8/20/2010 12:56 PM, M B wrote: fre 2010-08-20 klockan 13:19 -0600 skrev Burton Samograd: M B writes: Hi, dept=0 def mud(): print dept mud() 0 def mud(): dept+=1 print dept You should add a global statement or else python thinks a variable used is a

Re: scope of variable

2010-08-20 Thread M B
fre 2010-08-20 klockan 13:19 -0600 skrev Burton Samograd: > M B writes: > > > Hi, > dept=0 > def mud(): > > print dept > > > > > mud() > > 0 > def mud(): > > dept+=1 > > print dept > > You should add a global statement or else python thinks a variable used >

Re: scope of variable

2010-08-20 Thread Burton Samograd
M B writes: > Hi, dept=0 def mud(): > print dept > > mud() > 0 def mud(): > dept+=1 > print dept You should add a global statement or else python thinks a variable used is a local: >>> def mud(): global dept dept+=1 print dept -

Re: scope of variable

2010-08-20 Thread Rony
On Aug 20, 8:25 pm, Chris Rebert wrote: > On Fri, Aug 20, 2010 at 11:09 AM, M B wrote: > > Hi, > > I try to learn python. > > I don't understand this: > > dept=0 > > def mud(): > >        dept+=1 > >        print dept > > mud() > > Traceback (most recent call last): > >  File "",

Re: scope of variable

2010-08-20 Thread Chris Rebert
On Fri, Aug 20, 2010 at 11:09 AM, M B wrote: > Hi, > I try to learn python. > I don't understand this: dept=0 def mud(): >        dept+=1 >        print dept > mud() > Traceback (most recent call last): >  File "", line 1, in >    mud() >  File "", line 2, in mud >    dept+=1 > U

scope of variable

2010-08-20 Thread M B
Hi, I try to learn python. I don't understand this: (running in idle) >>> dept=0 >>> def mud(): print dept >>> mud() 0 >>> def mud(): dept+=1 print dept >>> mud() Traceback (most recent call last): File "", line 1, in mud() File "", line 2, in