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
> 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
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
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
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
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):
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()
;>>
>>> >>> 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
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
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
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
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
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
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... """
>
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-
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
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/
17 matches
Mail list logo