Re: exec() an locals() puzzle

2022-07-21 Thread george trojan
Thanks. That cdef-locals concept is consistent with the following example: def f(): i = 1 def g(): print('i' in globals(), 'i' in locals()) def h(): print('i' in globals(), 'i' in locals()); i g() h() f() False False False True It is a mystery, which may be why the documentat

Re: exec() an locals() puzzle

2022-07-20 Thread Martin Di Paola
I did a few tests # test 1 def f(): i = 1 print(locals()) exec('y = i; print(y); print(locals())') print(locals()) a = eval('y') print(locals()) u = a print(u) f() {'i': 1} 1 {'i': 1, 'y': 1} {'i': 1, 'y': 1} {'i': 1, 'y': 1, 'a': 1} 1 # test 2 def f(): i = 1

Re: exec() an locals() puzzle

2022-07-20 Thread Eryk Sun
On 7/20/22, george trojan wrote: > > 1. This works as I expect it to work: > > def f(): > i = 1 > print(locals()) > exec('y = i; print(y); print(locals())') > print(locals()) > exec('y *= 2') > print('ok:', eval('y')) > f() In CPython, the locals of a function scope (as op

Re: exec and globals and locals ...

2019-09-20 Thread jfong
Peter Otten於 2019年9月20日星期五 UTC+8下午3時31分48秒寫道: > jf...@ms4.hinet.net wrote: > > x = 3 > def foo(): > > ... exec("print(globals(), locals()); x = x + 1; print(globals(), > > locals())") ... > foo() > > {'foo': , '__package__': None, '__builtins__': > > {, '__loader__': > {'_froze

Re: exec and globals and locals ...

2019-09-20 Thread Peter Otten
jf...@ms4.hinet.net wrote: x = 3 def foo(): > ... exec("print(globals(), locals()); x = x + 1; print(globals(), > locals())") ... foo() > {'foo': , '__package__': None, '__builtins__': > {, '__loader__': {'_frozen_importlib.BuiltinImporter'>, '__doc__': None, '__name__': > {'__

Re: exec and globals and locals ...

2019-09-19 Thread jfong
>>> x = 3 >>> def foo(): ... exec("print(globals(), locals()); x = x + 1; print(globals(), locals())") ... >>> foo() {'foo': , '__package__': None, '__builtins__': , '__loader__': , '__doc__': None, '__name__': '__main__', '__spec__': None, 'x': 3} {} {'foo': , '__package__': None, '__builti

Re: exec and globals and locals ...

2019-09-19 Thread Eko palypse
Am Donnerstag, 19. September 2019 20:24:49 UTC+2 schrieb Peter Otten: > Eko palypse wrote: > > > Am Donnerstag, 19. September 2019 18:31:43 UTC+2 schrieb Peter Otten: > >> Eko palypse wrote: > >> > >> > No, I have to correct myself > >> > > >> > x = 5 > >> > def f1(): > >> > exec("x = x + 1;

Re: exec and globals and locals ...

2019-09-19 Thread Peter Otten
Eko palypse wrote: > Am Donnerstag, 19. September 2019 18:31:43 UTC+2 schrieb Peter Otten: >> Eko palypse wrote: >> >> > No, I have to correct myself >> > >> > x = 5 >> > def f1(): >> > exec("x = x + 1; print('f1 in:', x)") >> > return x >> > print('f1 out', f1()) >> > >> > results in t

Re: exec and globals and locals ...

2019-09-19 Thread Eko palypse
Am Donnerstag, 19. September 2019 18:31:43 UTC+2 schrieb Peter Otten: > Eko palypse wrote: > > > No, I have to correct myself > > > > x = 5 > > def f1(): > > exec("x = x + 1; print('f1 in:', x)") > > return x > > print('f1 out', f1()) > > > > results in the same, for me confusing, result

Re: exec and globals and locals ...

2019-09-19 Thread Eko palypse
First thank you for all the answers, very much appreciated. I assume the root cause might be explained by the zen of python as well. If the implementation is hard to explain, it's a bad idea. Maybe I need to rethink my implementation :-) Eren -- https://mail.python.org/mailman/listinfo/python-l

Re: exec and globals and locals ...

2019-09-19 Thread Peter Otten
Eko palypse wrote: > No, I have to correct myself > > x = 5 > def f1(): > exec("x = x + 1; print('f1 in:', x)") > return x > print('f1 out', f1()) > > results in the same, for me confusing, results. > > f1 in: 6 > f1 out 5 Inside a function exec assignments go to a *copy* of the local

Re: exec and globals and locals ...

2019-09-19 Thread Eko palypse
No, I have to correct myself x = 5 def f1(): exec("x = x + 1; print('f1 in:', x)") return x print('f1 out', f1()) results in the same, for me confusing, results. f1 in: 6 f1 out 5 Eren -- https://mail.python.org/mailman/listinfo/python-list

Re: exec and globals and locals ...

2019-09-19 Thread Peter Otten
Eko palypse wrote: > Thank you, I'm currently investigating importlib and read that > __builtins__ might be another alternative. > My ultimate goal would be to have objects available without the need to > import them, regardless whether used in a script directly or used in an > imported module. I

Re: exec and globals and locals ...

2019-09-19 Thread Peter Otten
Richard Damon wrote: > On 9/19/19 6:16 AM, Eko palypse wrote: >>> In all cases, if the optional parts are omitted, the code is executed in >>> the current scope. ... >>> >>> >>> You can see from it that "globals" is optional. >>> And that, if "globals" is missing, then >>> "exec" is executed in th

Re: exec and globals and locals ...

2019-09-19 Thread Bev In TX
I’m not the OP, but I want to thank you for that clarification. I had previously not understood the ramifications of the following in section “7. Simple statements” in “The Python Language Reference”: “An augmented assignment expression like x += 1 can be rewritten as x = x + 1 to achieve a si

Re: exec and globals and locals ...

2019-09-19 Thread Richard Damon
On 9/19/19 6:52 AM, Eko palypse wrote: > Am Donnerstag, 19. September 2019 12:45:35 UTC+2 schrieb Richard Damon: >> On 9/19/19 6:16 AM, Eko palypse wrote: In all cases, if the optional parts are omitted, the code is executed in the current scope. ... You can see from it th

Re: exec and globals and locals ...

2019-09-19 Thread Eko palypse
Am Donnerstag, 19. September 2019 12:56:59 UTC+2 schrieb Peter Otten: > Eko palypse wrote: > > >> Then it should be clear that the name 'test01' is put into globals(), if > >> load_module() doesn't throw an exception. No sharing or nesting of > >> namespaces takes place. > > > > Thank you too for

Re: exec and globals and locals ...

2019-09-19 Thread Peter Otten
Eko palypse wrote: >> Then it should be clear that the name 'test01' is put into globals(), if >> load_module() doesn't throw an exception. No sharing or nesting of >> namespaces takes place. > > Thank you too for your answer. Ok, that means that in every case when exec > imports something it has

Re: exec and globals and locals ...

2019-09-19 Thread Eko palypse
Am Donnerstag, 19. September 2019 12:45:35 UTC+2 schrieb Richard Damon: > On 9/19/19 6:16 AM, Eko palypse wrote: > >> In all cases, if the optional parts are omitted, the code is executed in > >> the current scope. ... > >> > >> > >> You can see from it that "globals" is optional. > >> And that, i

Re: exec and globals and locals ...

2019-09-19 Thread Richard Damon
On 9/19/19 6:16 AM, Eko palypse wrote: >> In all cases, if the optional parts are omitted, the code is executed in the >> current scope. ... >> >> >> You can see from it that "globals" is optional. >> And that, if "globals" is missing, then >> "exec" is executed in the current scope ("f1" in your

Re: exec and globals and locals ...

2019-09-19 Thread Eko palypse
> Then it should be clear that the name 'test01' is put into globals(), if > load_module() doesn't throw an exception. No sharing or nesting of > namespaces takes place. Thank you too for your answer. Ok, that means that in every case when exec imports something it has its own global namespace,

Re: exec and globals and locals ...

2019-09-19 Thread Eko palypse
> In all cases, if the optional parts are omitted, the code is executed in the > current scope. ... > > > You can see from it that "globals" is optional. > And that, if "globals" is missing, then > "exec" is executed in the current scope ("f1" in your case). Thank you for your answer, and that

Re: exec and globals and locals ...

2019-09-18 Thread Peter Otten
Eko palypse wrote: > exec('import test01', globals()) > print('f3 out', x) > > # result exception, expected but because f1 didn't throw an exception > # I'm confused. module test01 has only this two lines > x += 1 > print('f3 in:', x) The lines above run in the test01's global namespace, not in

Re: exec and globals and locals ...

2019-09-18 Thread dieter
Eko palypse writes: > Why does f1 work? I've expected an exception as no global dict has been > provided > ... >def f1(...): > exec("...") >... The documentation ("https://docs.python.org/3/library/functions.html#exec";) tells you: exec(object[, globals[, locals]]) ... In all cases, if the opt

Re: exec and traceback

2018-01-25 Thread Ned Batchelder
On 1/22/18 3:22 AM, ken...@gameofy.com wrote: I'm using exec() to run a (multi-line) string of python code. If an exception occurs, I get a traceback containing a stack frame for the string. I've labeled the code object with a "file name" so I can identify it easily, and when I debug, I find

Re: exec and traceback

2018-01-23 Thread dieter
ken...@gameofy.com writes: > I'm using exec() to run a (multi-line) string of python code. If an > exception occurs, I get a traceback containing a stack frame for the > string. I've labeled the code object with a "file name" so I can > identify it easily, and when I debug, I find that I can inter

Re: exec and traceback

2018-01-22 Thread Ned Batchelder
On 1/22/18 3:22 AM, ken...@gameofy.com wrote: (BTW, I've written a simple secure eval()) You have accurately guessed our interest!  Would you mind starting a new thread to show us your simple secure eval? --Ned. -- https://mail.python.org/mailman/listinfo/python-list

Re: exec and traceback

2018-01-22 Thread Chris Angelico
On Mon, Jan 22, 2018 at 7:22 PM, wrote: > > > I'm using exec() to run a (multi-line) string of python code. If an > exception occurs, I get a traceback containing a stack frame for the string. > I've labeled the code object with a "file name" so I can identify it easily, > and when I debug, I fin

Re: `exec`-based routine crashes app upon migration from 3.4.3 to python 3.5.2.

2016-08-01 Thread eryk sun
On Tue, Aug 2, 2016 at 1:45 AM, Lawrence D’Oliveiro wrote: > On Friday, July 29, 2016 at 6:25:51 AM UTC+12, Enjoys Math wrote: > >> exec('obj = ' + objType + '(self)', None, _locals) >> obj = _locals['obj'] > > Why? Why not just > > obj = objType(self) I think

Re: `exec`-based routine crashes app upon migration from 3.4.3 to python 3.5.2.

2016-08-01 Thread Lawrence D’Oliveiro
On Friday, July 29, 2016 at 6:25:51 AM UTC+12, Enjoys Math wrote: > exec('obj = ' + objType + '(self)', None, _locals) > obj = _locals['obj'] Why? Why not just obj = objType(self) ? -- https://mail.python.org/mailman/listinfo/python-list

Re: `exec`-based routine crashes app upon migration from 3.4.3 to python 3.5.2.

2016-07-28 Thread eryk sun
On Thu, Jul 28, 2016 at 6:40 PM, Chris Angelico wrote: > On Fri, Jul 29, 2016 at 1:47 AM, Enjoys Math wrote: >> I've manually set breakpoints and traced this app crash back to this >> function: >> >> def loadLSobjsOfType(self, objType, listJ): >> if listJ != None: >> for o

Re: `exec`-based routine crashes app upon migration from 3.4.3 to python 3.5.2.

2016-07-28 Thread Random832
On Thu, Jul 28, 2016, at 11:47, Enjoys Math wrote: > So what's the proper way to get the return value of an exec call when > there is one? Exec calls do not have return values. If you need to pass an object out of the exec call to the surrounding context, you can wrap it in an exception and throw

Re: `exec`-based routine crashes app upon migration from 3.4.3 to python 3.5.2.

2016-07-28 Thread Chris Angelico
On Fri, Jul 29, 2016 at 1:47 AM, Enjoys Math wrote: > I've manually set breakpoints and traced this app crash back to this > function: > > def loadLSobjsOfType(self, objType, listJ): > if listJ != None: > for objJ in listJ: > _locals = locals() >

Re: exec inside functions in Python 3

2016-03-22 Thread Chris Angelico
On Tue, Mar 22, 2016 at 11:57 PM, Steven D'Aprano wrote: > Anyone have any idea what is going on here? > > > def test(): > spam = 1 > exec("spam = 2; print('inside exec: %d' % spam)") > print('outside exec: %d' % spam) > > > In Python 2.7: > > py> test() > inside exec: 2 > outside exec

Re: exec "x = 3; print x" in a - How does it work?

2016-03-08 Thread Chris Angelico
On Wed, Mar 9, 2016 at 5:25 PM, Veek. M wrote: > ah, okay - i'm familiar with the py3 syntax where you do: > eval('whatever stmt', globals={}, locals={}) > I suppose this: exec " " in NS; syntax is strictly py2? > > Many thanks :) Yeah, that's one of the things that was cleaned up in Py3. Several

Re: exec "x = 3; print x" in a - How does it work?

2016-03-08 Thread Veek. M
Steven D'Aprano wrote: > On Wednesday 09 March 2016 16:27, Veek. M wrote: > >> What is the return value of `exec`? Would that object be then used to >> iterate the sequence in 'a'? I'm reading this: >> https://www.python.org/download/releases/2.2.3/descrintro/ > > > exec is a statement, not a f

Re: exec "x = 3; print x" in a - How does it work?

2016-03-08 Thread Veek. M
Ben Finney wrote: > "Veek. M" writes: > >> What is the return value of `exec`? > > You can refer to the documentation for questions like that. > https://docs.python.org/3/library/functions.html#exec> > >> Would that object be then used to iterate the sequence in 'a'? > > The ‘for’ or ‘while’

Re: exec "x = 3; print x" in a - How does it work?

2016-03-08 Thread Steven D'Aprano
On Wednesday 09 March 2016 16:27, Veek. M wrote: > What is the return value of `exec`? Would that object be then used to > iterate the sequence in 'a'? I'm reading this: > https://www.python.org/download/releases/2.2.3/descrintro/ exec is a statement, not a function, so it doesn't have a return

Re: exec "x = 3; print x" in a - How does it work?

2016-03-08 Thread Ben Finney
"Veek. M" writes: > What is the return value of `exec`? You can refer to the documentation for questions like that. https://docs.python.org/3/library/functions.html#exec> > Would that object be then used to iterate the sequence in 'a'? The ‘for’ or ‘while’ statements are correct for iteration.

Re: exec and locals

2014-02-27 Thread Steven D'Aprano
On Fri, 28 Feb 2014 00:29:35 +1300, Gregory Ewing wrote: > Steven D'Aprano wrote: >> On Thu, 27 Feb 2014 16:34:33 +1300, Gregory Ewing wrote: >> >>>Why not just use this version all the time? It should work in both 2.x >>>and 3.x. >> >> Because that's yucky. It's an aesthetic thing: when support

Re: exec and locals

2014-02-27 Thread Chris Angelico
On Thu, Feb 27, 2014 at 10:29 PM, Gregory Ewing wrote: > Steven D'Aprano wrote: >> >> On Thu, 27 Feb 2014 16:34:33 +1300, Gregory Ewing wrote: >> >>> Why not just use this version all the time? It should work in both 2.x >>> and 3.x. >> >> >> Because that's yucky. It's an aesthetic thing: when sup

Re: exec and locals

2014-02-27 Thread Gregory Ewing
Steven D'Aprano wrote: On Thu, 27 Feb 2014 16:34:33 +1300, Gregory Ewing wrote: Why not just use this version all the time? It should work in both 2.x and 3.x. Because that's yucky. It's an aesthetic thing: when supported, I want the Python interpreter to manage the context manager. More y

Re: exec and locals

2014-02-27 Thread Alister
On Thu, 27 Feb 2014 00:31:56 +, Steven D'Aprano wrote: > On Wed, 26 Feb 2014 14:00:59 +, Alister wrote: > >> On Wed, 26 Feb 2014 13:15:25 +, Steven D'Aprano wrote: >> >>> I have to dynamically generate some code inside a function using exec, >>> but I'm not sure if it is working by a

Re: exec and locals

2014-02-26 Thread Chris Angelico
On Thu, Feb 27, 2014 at 3:47 PM, Steven D'Aprano wrote: > Guys, I know that exec is kinda dangerous and newbies should be > discouraged from throwing every string they see at it, but this isn't my > second day Python programming, and it's not an accident that Python > supports the dynamic compilat

Re: exec and locals

2014-02-26 Thread Steven D'Aprano
On Wed, 26 Feb 2014 23:20:10 -0500, Dave Angel wrote: > Before I would use exec, I'd look hard at either generating a > source file to import, Yes, I went through the process of pulling out the code into a separate module, but that just made more complexity and was pretty nasty. If the func

Re: exec and locals

2014-02-26 Thread Steven D'Aprano
On Thu, 27 Feb 2014 16:34:33 +1300, Gregory Ewing wrote: > Steven D'Aprano wrote: >> except SyntaxError: >> def inner(): >> # manually operate the context manager call context manager >> __enter__ >> try: >> try: >>

Re: exec and locals

2014-02-26 Thread Dave Angel
Steven D'Aprano Wrote in message: > On Wed, 26 Feb 2014 14:46:39 +0100, Peter Otten wrote: > >> Steven D'Aprano wrote: >> >>> I have to dynamically generate some code inside a function using exec, >>> but I'm not sure if it is working by accident or if I can rely on it. >>> >> I eventually set

Re: exec and locals

2014-02-26 Thread Dan Sommers
On Thu, 27 Feb 2014 00:25:45 +, Steven D'Aprano wrote: > By the way, if anyone cares what my actual use-case is, I have a > function that needs to work under Python 2.4 through 3.4, and it uses > a with statement. With statements are not available in 2.4 (or 2.5, > unless you give a from __fut

Re: exec and locals

2014-02-26 Thread Gregory Ewing
Steven D'Aprano wrote: except SyntaxError: def inner(): # manually operate the context manager call context manager __enter__ try: try: return something except: # Yes, a bare except. Catch EVERYTH

Re: exec and locals

2014-02-26 Thread Steven D'Aprano
On Wed, 26 Feb 2014 14:00:59 +, Alister wrote: > On Wed, 26 Feb 2014 13:15:25 +, Steven D'Aprano wrote: > >> I have to dynamically generate some code inside a function using exec, >> but I'm not sure if it is working by accident or if I can rely on it. [...] > I have no idea but as exec

Re: exec and locals

2014-02-26 Thread Steven D'Aprano
On Wed, 26 Feb 2014 14:46:39 +0100, Peter Otten wrote: > Steven D'Aprano wrote: > >> I have to dynamically generate some code inside a function using exec, >> but I'm not sure if it is working by accident or if I can rely on it. >> >> Here is a trivial example: >> >> >> py> def spam(): >> ...

Re: exec and locals

2014-02-26 Thread Alister
On Wed, 26 Feb 2014 13:15:25 +, Steven D'Aprano wrote: > I have to dynamically generate some code inside a function using exec, > but I'm not sure if it is working by accident or if I can rely on it. > > Here is a trivial example: > > > py> def spam(): > ... exec( """x = 23""" ) > ...

Re: exec and locals

2014-02-26 Thread Peter Otten
Peter Otten wrote: > Steven D'Aprano wrote: > >> I have to dynamically generate some code inside a function using exec, >> but I'm not sure if it is working by accident or if I can rely on it. >> >> Here is a trivial example: >> >> >> py> def spam(): >> ... exec( """x = 23""" ) >> ...

Re: exec and locals

2014-02-26 Thread Peter Otten
Steven D'Aprano wrote: > I have to dynamically generate some code inside a function using exec, > but I'm not sure if it is working by accident or if I can rely on it. > > Here is a trivial example: > > > py> def spam(): > ... exec( """x = 23""" ) > ... return x > ... > py> spam() > 23

Re: exec and locals

2014-02-26 Thread Chris Angelico
On Thu, Feb 27, 2014 at 12:15 AM, Steven D'Aprano wrote: > py> def spam(): > ... exec( """x = 23""" ) > ... return x > ... > py> spam() > 23 > > > (My real example is more complex than this.) > > According to the documentation of exec, I don't think this should > actually work, and yet it

Re: exec with partial globals

2012-10-30 Thread Dave Angel
On 10/30/2012 08:57 AM, Helmut Jarausch wrote: > On Tue, 30 Oct 2012 08:33:38 -0400, Dave Angel wrote: > >> On 10/30/2012 08:00 AM, Helmut Jarausch wrote: >>> Hi, >>> >>> I'd like to give the user the ability to enter code which may only rebind >>> a given set of names but not all ones. >>> This do

Re: exec with partial globals

2012-10-30 Thread Chris Angelico
On Tue, Oct 30, 2012 at 11:57 PM, Helmut Jarausch wrote: > Given spreadsheet S (Source) and D (Destination) as objects (wrapping a > dictionary) a possible (legal) input would be > > D.price= D.price-S.discount > > No other fields of 'D' should be modifiable. That's a bit harder. What you're des

Re: exec with partial globals

2012-10-30 Thread Helmut Jarausch
On Tue, 30 Oct 2012 08:33:38 -0400, Dave Angel wrote: > On 10/30/2012 08:00 AM, Helmut Jarausch wrote: >> Hi, >> >> I'd like to give the user the ability to enter code which may only rebind >> a given set of names but not all ones. >> This does NOT work >> A=1 >> B=2 >> Code=compile('A=7','','exec

Re: exec with partial globals

2012-10-30 Thread Dave Angel
On 10/30/2012 08:00 AM, Helmut Jarausch wrote: > Hi, > > I'd like to give the user the ability to enter code which may only rebind > a given set of names but not all ones. > This does NOT work > A=1 > B=2 > Code=compile('A=7','','exec') > exec(Code,{'A':0}) > print("I've got A={}".format(A)) # prin

Re: exec with partial globals

2012-10-30 Thread Chris Angelico
On Tue, Oct 30, 2012 at 11:00 PM, Helmut Jarausch wrote: > Hi, > > I'd like to give the user the ability to enter code which may only rebind > a given set of names but not all ones. > > How can 'filter' the gobal namespace such that modifying 'A' is allowed > but any attempt to modify 'B' should g

Re: exec

2012-03-01 Thread Peter Otten
Prasad, Ramit wrote: > Hi Peter, > > >>> class Magnitude(object): > > ... def __init__(self, value): > ... self.value = value > ... def __call__(self, uf=1): > ... if uf == 1: > ... return self > ... return self.value > ... > > >>>

Re: exec

2012-03-01 Thread Rolf Wester
Thank you, that really made things much easier and admittedly much less nasty too. Regards Rolf On 01/03/12 18:14, Peter Otten wrote: > Rolf Wester wrote: > >> The reason to use exec is just laziness. I have quite a lot of classes >> representing material data and every class has a number of pa

Re: exec

2012-03-01 Thread Peter Otten
Rolf Wester wrote: > The reason to use exec is just laziness. I have quite a lot of classes > representing material data and every class has a number of parameters. > The parameter are Magnitude objects (they have a value and a unit and > overloaded special functions to correctly handle the units)

Re: exec

2012-03-01 Thread Michael Ströder
Rolf Wester wrote: The reason to use exec is just laziness. The worst reason for using it. So I hope you carefully read Steven's comment and get rid of exec() for anything serious: <4f4f85eb$0$29989$c3e8da3$54964...@news.astraweb.com> Ciao, Michael. -- http://mail.python.org/mailman/listinf

Re: exec

2012-03-01 Thread Rolf Wester
Hi, thanks for your replies so far. The reason to use exec is just laziness. I have quite a lot of classes representing material data and every class has a number of parameters. The parameter are Magnitude objects (they have a value and a unit and overloaded special functions to correctly handle

Re: exec

2012-03-01 Thread Steven D'Aprano
On Thu, 01 Mar 2012 14:07:15 +0100, Rolf Wester wrote: > Hi, > > I would like to define methods using exec like this: > > class A: > def __init__(self): > cmd = "def sqr(self, x):\nreturn x**2\nself.sqr = sqr\n" > exec cmd > a = A() > print a.sqr(a, 2) That's... nasty,

Re: exec

2012-03-01 Thread Arnaud Delobelle
On 1 March 2012 13:07, Rolf Wester wrote: > Hi, > > I would like to define methods using exec like this: > > class A: >    def __init__(self): >        cmd = "def sqr(self, x):\n    return x**2\nself.sqr = sqr\n" >        exec cmd > a = A() > print a.sqr(a, 2) > > This works, but I have to call sq

Re: exec

2012-03-01 Thread Jean-Michel Pichavant
Rolf Wester wrote: Hi, I would like to define methods using exec like this: class A: def __init__(self): cmd = "def sqr(self, x):\nreturn x**2\nself.sqr = sqr\n" exec cmd a = A() print a.sqr(a, 2) This works, but I have to call sqr with a.sqr(a, 2), a.sqr(2) does not wo

Re: exec, import and isinstance

2011-02-08 Thread Peter Otten
Michele Petrazzo wrote: > Hi all, > I'm playing with python internals and I'm discovering a strange behavior > of isinstance. Considering the following test case: > > test/bar.py > test/b.py > test/a/__init__.py > test/a/foo.py > > -- __init__.py -> empty > > --- foo.py: > class foo: pass > c =

Re: exec .exe

2010-03-26 Thread wukong
On Mar 26, 3:10 pm, Irmen de Jong wrote: > On 26-3-2010 22:58, wukong wrote: > > > newbie question, how do you run a .exe generated by MSVC++ in python > > in windows? > > Use the subprocess module, for instance: > subprocess.call(["notepad.exe", "d:/file.txt"]) > > irmen worked like a charm, :-)

Re: exec .exe

2010-03-26 Thread Irmen de Jong
On 26-3-2010 22:58, wukong wrote: newbie question, how do you run a .exe generated by MSVC++ in python in windows? Use the subprocess module, for instance: subprocess.call(["notepad.exe", "d:/file.txt"]) irmen -- http://mail.python.org/mailman/listinfo/python-list

Re: exec within function

2010-02-03 Thread Peter Otten
Gerald Britton wrote: > On Wed, Feb 3, 2010 at 2:59 PM, Terry Reedy wrote: >> On 2/3/2010 3:30 AM, Simon zack wrote: >>> >>> hi, >>> I'm not sure how I can use exec within a function correctly >>> here is the code i'm using: >>> >>> def a(): >>> exec('b=1') >>> print(b) >>> >>> a() >>> >>> this w

Re: exec within function

2010-02-03 Thread Gerald Britton
I get no error: >>> def a(): ... exec('b=1') ... print(b) ... >>> a() 1 >>> On Wed, Feb 3, 2010 at 2:59 PM, Terry Reedy wrote: > On 2/3/2010 3:30 AM, Simon zack wrote: >> >> hi, >> I'm not sure how I can use exec within a function correctly >> here is the code i'm using: >> >> def a(): >>    

Re: exec within function

2010-02-03 Thread Terry Reedy
On 2/3/2010 3:30 AM, Simon zack wrote: hi, I'm not sure how I can use exec within a function correctly here is the code i'm using: def a(): exec('b=1') print(b) a() this will raise an error, but I would like to see it outputting 1 Always **copy and paste** **complete error tracebac

Re: Exec Statement Question

2009-11-30 Thread Victor Subervi
On Mon, Nov 30, 2009 at 1:12 PM, Dave Angel wrote: > Victor Subervi wrote: > >> On Sun, Nov 29, 2009 at 10:23 PM, Dave Angel wrote: >> >> >> >>> exec is a statement, and statements don't have "return values." It's >>> not >>> a function, so there are no parentheses in its syntax, either. exec

Re: Exec Statement Question

2009-11-30 Thread Victor Subervi
On Mon, Nov 30, 2009 at 12:25 PM, Carsten Haese wrote: > Victor Subervi wrote: > > Taking out the parenthesis did it! Thanks. Now, you state this is an > > option of last resort. Although this does indeed achieve my desired aim, > > here is a complete example of what I am trying to achieve. The fo

Re: Exec Statement Question

2009-11-30 Thread Dave Angel
Victor Subervi wrote: On Sun, Nov 29, 2009 at 10:23 PM, Dave Angel wrote: exec is a statement, and statements don't have "return values." It's not a function, so there are no parentheses in its syntax, either. exec is also a technique of last resort; there's nearly always a better/safer

Re: Exec Statement Question

2009-11-30 Thread Carsten Haese
Victor Subervi wrote: > Taking out the parenthesis did it! Thanks. Now, you state this is an > option of last resort. Although this does indeed achieve my desired aim, > here is a complete example of what I am trying to achieve. The following > is from 'createTables2.py': > > for table in tables

Re: Exec Statement Question

2009-11-30 Thread Marco Mariani
Jean-Michel Pichavant wrote: if which == '': i = 0 all = '' while i < len(meanings): table = '%s\n' % meanings[i] table += "\n \n composing HTML like that is painful, bug prone and insecure You should have a look at http://tottinge.blogsome.com/meaningfulnames/ I

Re: Exec Statement Question

2009-11-30 Thread Jean-Michel Pichavant
Victor Subervi wrote: if which == '': i = 0 all = '' while i < len(meanings): table = '%s\n' % meanings[i] table += "\n \n align='center'>%s\n " % names[i] j = 0 for elt in code: if (j + 8) % 8 == 0: table += ' \n' table += ' %s\

Re: Exec Statement Question

2009-11-30 Thread Victor Subervi
On Sun, Nov 29, 2009 at 10:23 PM, Dave Angel wrote: > exec is a statement, and statements don't have "return values." It's not > a function, so there are no parentheses in its syntax, either. exec is also > a technique of last resort; there's nearly always a better/safer/faster way > to accom

Re: Exec Statement Question

2009-11-29 Thread Terry Reedy
Dave Angel wrote: exec is a statement, and statements don't have "return values." It's not a function, In 3.x, it is a function, though the use of print as a statement indicates that the OP was using 2.x. -- http://mail.python.org/mailman/listinfo/python-list

Re: Exec Statement Question

2009-11-29 Thread Dave Angel
Victor Subervi wrote: Hi; I have the following line of code: exec('%s()' % table) where 'table' is a variable in a for loop that calls variables from another script. I've made it so that it only calls one variable. I know for a fact that it's calling that variable in the script because it found

Re: exec-function in Python 3.+

2009-11-02 Thread Dave Angel
Hans Larsen wrote: Help! I'm begginer in Python 3.+! If i wih to update a module after an import and chages, How could I do: By "from imp import reload" and then reload(mymodule) or how to use "exec(?)", it is mentoined in docs. In Python ver. <3 reload(module) writes some

Re: exec-function in Python 3.+

2009-11-02 Thread Jon Clements
On 2 Nov, 10:49, "Hans Larsen" wrote: > Help! >     I'm begginer in Python 3.+! >     If i wih to update a module after an import and chages, >     How could I do: >     By "from imp import reload" and then reload(mymodule) >     or how to use "exec(?)", it is mentoined in docs. >     In Python ve

Re: exec globals and locals

2009-09-02 Thread John Nagle
Chris Rebert wrote: On Wed, Sep 2, 2009 at 4:54 AM, Quentin Lampin wrote: Hi, Being fairly new to Python, I'm trying to figure out the best way to use the exec statement and I must admit that I am a bit lost. Generally, if you want to use the exec statement, you're probably lost. Unless yo

Re: exec globals and locals

2009-09-02 Thread Quentin Lampin
2009/9/2 Chris Rebert > On Wed, Sep 2, 2009 at 1:13 PM, Quentin Lampin > wrote: > > 2009/9/2 Chris Rebert > >> > >> On Wed, Sep 2, 2009 at 4:54 AM, Quentin Lampin > > >> wrote: > >> > Hi, > >> > Being fairly new to Python, I'm trying to figure out the best way to > use > >> > the > >> > exec sta

Re: exec globals and locals

2009-09-02 Thread Chris Rebert
On Wed, Sep 2, 2009 at 1:13 PM, Quentin Lampin wrote: > 2009/9/2 Chris Rebert >> >> On Wed, Sep 2, 2009 at 4:54 AM, Quentin Lampin >> wrote: >> > Hi, >> > Being fairly new to Python, I'm trying to figure out the best way to use >> > the >> > exec statement and I must admit that I am a bit lost. >>

Re: exec globals and locals

2009-09-02 Thread Quentin Lampin
2009/9/2 Chris Rebert > On Wed, Sep 2, 2009 at 4:54 AM, Quentin Lampin > wrote: > > Hi, > > Being fairly new to Python, I'm trying to figure out the best way to use > the > > exec statement and I must admit that I am a bit lost. > > > > Consider this case: > > exec "print 'a'" in {},{} [exp.1]

Re: exec globals and locals

2009-09-02 Thread Chris Rebert
On Wed, Sep 2, 2009 at 4:54 AM, Quentin Lampin wrote: > Hi, > Being fairly new to Python, I'm trying to figure out the best way to use the > exec statement and I must admit that I am a bit lost. > > Consider this case: > exec "print 'a'" in {},{}   [exp.1] > It means  that I'm (kindly) asking the i

Re: exec("dir()",d)

2009-08-10 Thread Emanuele D'Arrigo
Thank you both for the explanation. As a matter of fact RTFM doesn't -always- help. Sometimes I'm just thick and I can read the manual 10 times and still not understand, as it happened on this particular matter. Your contribution focused my attention on the right bit of the manual which I somehow

Re: exec("dir()",d)

2009-08-09 Thread Christian Heimes
Emanuele D'Arrigo schrieb: Greetings everybody, I don't quite understand why if I do this: d = {} exec("dir()", d) 1) d is no longer empty 2) the content of d now looks like __builtins__.__dict__ but isn't quite it d == __builtins__.__dict__ returns false. Can anybody shed some light? RTF

Re: exec("dir()",d)

2009-08-09 Thread Mel
Emanuele D'Arrigo wrote: > Greetings everybody, > > I don't quite understand why if I do this: > d = {} exec("dir()", d) > > 1) d is no longer empty > 2) the content of d now looks like __builtins__.__dict__ but isn't > quite it d == __builtins__.__dict__ returns false. > > Can anybo

Re: exec statement with/without prior compile...

2009-05-26 Thread Arnaud Delobelle
"Gabriel Genellina" writes: > En Mon, 25 May 2009 11:07:13 -0300, Jaime Fernandez del Rio > escribió: >> And since I'm asking, could something similar, i.e. defining a new >> function and get a handle to it, be achieved with eval? > > No, because def is a statement, and eval only accepts an expr

Re: exec statement with/without prior compile...

2009-05-26 Thread Gabriel Genellina
En Mon, 25 May 2009 11:07:13 -0300, Jaime Fernandez del Rio escribió: These weekend I've been tearing down to pieces Michele Simionato's decorator module, that builds signature preserving decorators. At the heart of it all there is a dynamically generated function, which works something similar

Re: Exec woes

2009-01-29 Thread Rhodri James
On Thu, 29 Jan 2009 08:15:57 -, Hendrik van Rooyen wrote: "Rhodri James" wrote: To: Sent: Thursday, January 29, 2009 6:12 AM Subject: Re: Exec woes On Wed, 28 Jan 2009 07:47:00 -, Hendrik van Rooyen wrote: > This is actually not correct - it is the root cause

Re: Exec woes

2009-01-29 Thread Hendrik van Rooyen
"Rhodri James" wrote: To: Sent: Thursday, January 29, 2009 6:12 AM Subject: Re: Exec woes > On Wed, 28 Jan 2009 07:47:00 -, Hendrik van Rooyen > wrote: > > This is actually not correct - it is the root cause of my trouble. > > if you write, in a nested scope

Re: Exec woes

2009-01-28 Thread Rhodri James
On Wed, 28 Jan 2009 07:47:00 -, Hendrik van Rooyen wrote: Stephen Hansen wrote: Exec is a statement, not a function nor an object: even though you can enclose parens around its arguments like you do later on, they don't have any syntax meaning This is actually not correct - it is th

Re: Exec woes

2009-01-27 Thread Hendrik van Rooyen
Stephen Hansen wrote: >Hendrik van Rooyen wrote: >>IDLE 1.1.3 No Subprocess > help(exec) >>SyntaxError: invalid syntax > >>Its the same under Linux SuSe, Python 2.5.1. >> >>I think this is a BUG. > >Exec is a statement, not a function nor an object: even though you can enc

Re: Exec woes

2009-01-27 Thread Chris Rebert
On Tue, Jan 27, 2009 at 9:34 AM, Christian Heimes wrote: > Hendrik van Rooyen schrieb: >> It starts with the conspiracy of silence at the interactive prompt: >> >> Python 2.4.3 (#69, Mar 29 2006, 17:35:34) [MSC v.1310 32 bit (Intel)] on >> win32 >> Type "copyright", "credits" or "license()" for m

  1   2   >