Re: [mdi...@grulic.org.ar: modifying locals]

2015-10-13 Thread dieter
Marcos Dione writes: > ... > My problem is modifying the > locals ... In Python 2.7, I succeeded with the following code: >>> def f(): ... x = 1 ... exec('x=2') ... return x ... >>> f() 2 -- https://mail.python.org/mailman/listinfo/python-list

Re: [mdi...@grulic.org.ar: modifying locals]

2015-10-12 Thread Chris Angelico
> exec(), it's not just a matter of modifying locals()[2]. Short answer: You can't. Modifying locals is not something that's ever supported (except in the trivial case where locals() is globals(), as mutating globals() *is* supported). Personally, I'd be inclined to switc

[mdi...@grulic.org.ar: modifying locals]

2015-10-12 Thread Marcos Dione
I repost this here, as somebody in python-help told it was probably out of their league. - Forwarded message from Marcos Dione - Date: Sun, 11 Oct 2015 15:30:05 +0200 From: Marcos Dione Subject: modifying locals To: h...@python.org Message-ID: <20151011133005.GC2

Re: yet another modifying locals() question

2009-09-13 Thread Ed Anuff
On Sep 13, 8:15 pm, Steven D'Aprano wrote: > Metaclasses are left as an exercise for the reader. The parent class has a metaclass, which is why I was trying this approach instead, since it let me get at the class attributes before the metaclass did. Overriding the metaclass looked to be a much m

Re: yet another modifying locals() question

2009-09-13 Thread Steven D'Aprano
ied something out that seems to work but I'm not sure that it's > kosher: Given that the docs state not to rely on modifying locals(), I think it is safe to say it's NOT kosher. If it works for you, you're lucky, but it might stop working in the future. >>>> def

yet another modifying locals() question

2009-09-13 Thread Ed Anuff
I know that locals() is not supposed to be modifiable under most circumstances, but I'm trying to solve a situation where I'm dynamically generating some class attributes and it seemed to be the best way, so I tried something out that seems to work but I'm not sure that it's kosher: >>> def f(l):

Re: modifying locals

2008-10-31 Thread Tino Wildenhain
M.-A. Lemburg wrote: On 2008-10-31 09:08, Tino Wildenhain wrote: ... Ah thats interesting. I would not know because I usually avoid such ugly hacks :-) It doesn't even work for already defined local variables: def foo(): ... x = 1 ... locals()['x'] = 2 ... print x ... foo()

Re: modifying locals

2008-10-31 Thread M.-A. Lemburg
;>> >>> >>> locals()["foo"]="bar" >>> >>> foo >>> 'bar' >>> >> >> That is incorrect. People often try modifying locals() in the global >> scope, and then get bitten when it doesn't work

Re: modifying locals

2008-10-31 Thread Arnaud Delobelle
On Oct 30, 9:21 pm, "John [H2O]" <[EMAIL PROTECTED]> wrote: > I would like to write a function to write variables to a file and modify a > few 'counters'. This is to replace multiple instances of identical code in a > module I am writing. > > This is my approach: > > def write_vars(D): >     """ pa

Re: modifying locals

2008-10-31 Thread Tino Wildenhain
Hi, Steven D'Aprano wrote: On Fri, 31 Oct 2008 07:10:05 +0100, Tino Wildenhain wrote: Also, locals() already returns a dict, no need for the exec trickery. You can just modify it: >>> locals()["foo"]="bar" >>> foo 'bar' That is

Re: modifying locals

2008-10-31 Thread Steven D'Aprano
On Fri, 31 Oct 2008 07:10:05 +0100, Tino Wildenhain wrote: > Also, locals() already returns a dict, no need for the exec trickery. > You can just modify it: > > >>> locals()["foo"]="bar" > >>> foo > 'bar' > That is incorrec

Re: modifying locals

2008-10-31 Thread Marc 'BlackJack' Rintsch
On Thu, 30 Oct 2008 16:19:11 -0700, John [H2O] wrote: > Steven D'Aprano-7 wrote: >> >> What you are actually trying to do is unclear to me. Perhaps you could >> try explaining better with a more concrete example? >> > Actually, maybe a LACK of an example would make it simpler. What I'm > after i

Re: modifying locals

2008-10-30 Thread Tino Wildenhain
Hi John, John [H2O] wrote: Steven D'Aprano-7 wrote: What you are actually trying to do is unclear to me. Perhaps you could try explaining better with a more concrete example? -- Steven -- Actually, maybe a LACK of an example would make it simpler. What I'm after is a function, to which I

Re: modifying locals

2008-10-30 Thread Steve Holden
John [H2O] wrote: > I would like to write a function to write variables to a file and modify a > few 'counters'. This is to replace multiple instances of identical code in a > module I am writing. > > This is my approach: > > def write_vars(D): > """ pass D=locals() to this function... """ >

Re: modifying locals

2008-10-30 Thread John [H2O]
fied... then, #script D=locals() myFunction(D) As a point.. I thought I read somewhere that D.iteritems wasn't going to be available in Python3 so I've been trying to 'ween' myself from it. Thanks! -- View this message in context: http://www.nabble.com/modifying-locals-

Re: modifying locals

2008-10-30 Thread Steven D'Aprano
On Thu, 30 Oct 2008 14:21:01 -0700, John [H2O] wrote: > I would like to write a function to write variables to a file and modify > a few 'counters'. Are you talking about a function to generate Python source code? > This is to replace multiple instances of identical > code in a module I am wri

modifying locals

2008-10-30 Thread John [H2O]
at the modified values are reflected in the script. How do I do this? Using global? But that seems a bit dangerous since I am using exec. Bringing up another matter... is there a better way to do this that doesn't use exec? Thanks! -- View this message in context: http://www.nabble.com/