setting variables in the local namespace

2009-10-13 Thread Chris Withers
Hi All, Say I have a piece of code like this: mname = model.__name__ fname = mname+'_order' value = request.GET.get('order') if value: request.session[fname]=value else: value = request.session.get( fname,

Re: setting variables in the local namespace

2009-10-13 Thread Gabriel Genellina
En Tue, 13 Oct 2009 13:05:03 -0300, Chris Withers ch...@simplistix.co.uk escribió: What I'd be looking for is something like: locals()[name]=value ...or, say: setattr(somethingspecial,name,value) Now, I got horribly flamed for daring to be so heretical as to suggest this might be a

Re: setting variables in the local namespace

2009-10-13 Thread Mick Krippendorf
Hello. Chris Withers schrieb: mname = model.__name__ fname = mname+'_order' value = request.GET.get('order') if value: request.session[fname]=value else: value = request.session.get( fname,

Re: setting variables in the local namespace

2009-10-13 Thread Duncan Booth
Chris Withers ch...@simplistix.co.uk wrote: Now, if I want to do *exactly* the same thing with a variable named 'sort', I have to copy and paste the above code or do something hacky like have a dict called vars and manipulate that, or factor the above into a function and take the hit on

Re: setting variables in the local namespace

2009-10-13 Thread Carl Banks
On Oct 13, 9:05 am, Chris Withers ch...@simplistix.co.uk wrote: Hi All, Say I have a piece of code like this:          mname = model.__name__          fname = mname+'_order'          value = request.GET.get('order')          if value:              request.session[fname]=value          

Re: setting variables in the local namespace

2009-10-13 Thread Mel
Chris Withers wrote: - what is so wrong with wanting to set a variable in the local namespace based on a name stored in a variable? What's wrong is that no other statement using the local name space can know what that name might be. It's a documented fact that changing the locals()

Re: setting variables in the local namespace

2009-10-13 Thread Carl Banks
On Oct 13, 9:39 am, Mick Krippendorf mad.m...@gmx.de wrote: Yes, and, uh, yes. locals()['foo'] = bar works in that it does the same thing as foo = bar. So why don't you write that instead? Lemme guess. You tried this at the interactive prompt and concluded it worked in general, right? Even

Re: setting variables in the local namespace

2009-10-13 Thread Dave Angel
Mick Krippendorf wrote: snip Yes, and, uh, yes. locals()['foo'] = bar works in that it does the same thing as foo = bar. So why don't you write that instead? Mick. I wouldn't expect it to do the same thing at all, and it doesn't, at least not in Python 2.6.2. It may store the bar

Re: setting variables in the local namespace

2009-10-13 Thread Peter Pearson
On Tue, 13 Oct 2009 17:05:03 +0100, Chris Withers wrote: [snip] - what is so wrong with wanting to set a variable in the local namespace based on a name stored in a variable? I'm not sure it's so wrong that one should never, ever do it, but it *does* blur the boundary between the program and

Re: setting variables in the local namespace

2009-10-13 Thread Mick Krippendorf
Carl Banks schrieb: Lemme guess. You tried this at the interactive prompt and concluded it worked in general, right? Yes. Thank you for enlighten me. One of these days we're going to have a thread like this where no one makes this mistake. Don't know when, but one day it will happen.