Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #28147

Re: get return or locals from "exec" str in "environment"

References <5c488ca5-d730-40c4-b860-7a53201f21ae@googlegroups.com> <mailman.3987.1346365155.4697.python-list@python.org> <599e5709-22ab-4e85-8443-181a2ae19c17@googlegroups.com>
Date 2012-08-31 08:45 +1000
Subject Re: get return or locals from "exec" str in "environment"
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.3991.1346366717.4697.python-list@python.org> (permalink)

Show all headers | View raw


On Fri, Aug 31, 2012 at 8:25 AM, lucas <sjlukacs@gmail.com> wrote:
>> Far as I can see, you never actually called that function anywhere.
>> ChrisA
>
> doesn't the exec command call the function?

(Side point: You don't have to post to both comp.lang.python and
python-list - they mirror each other.)

What you executed included the 'def' statement. That's an executable
statement that creates a function object:

'lucas53': <function lucas53 at 0x214348c>

In Python, functions are objects just like dictionaries, strings, and
integers; you can construct them (usually with 'def' or 'lambda'),
pass them around, tinker with their attribututes, and ultimately, call
them.

But unless you do actually call that function, none of its code will
be executed. You can test this by putting a 'print' inside the
function; you'll see screen output when the function's called, and
your code above won't show that.

To access the local variables/names from the function itself, you'll
need to put a call to locals() inside that function, because as soon
as it finishes, those locals disappear. Try this:

>>> xx2 = """
def lucas53():
	harry = (4+16) / 2
	rtn = dict(harry=harry)
	return rtn

foo = lucas53()
"""
>>> env = {}
>>> exec(xx2,env)

(This is Python 3 syntax, exec is now a function - otherwise
equivalent to what you did.)

You'll now see a name 'foo' in env, with the mapping returned from
your function. There's no peeking into locals here, just a straight
function return value.

Is this what you were looking for?

ChrisA

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Re: get return or locals from "exec" str in "environment" Chris Angelico <rosuav@gmail.com> - 2012-08-31 08:19 +1000
  Re: get return or locals from "exec" str in "environment" lucas <sjlukacs@gmail.com> - 2012-08-30 15:25 -0700
    Re: get return or locals from "exec" str in "environment" Chris Angelico <rosuav@gmail.com> - 2012-08-31 08:45 +1000
      Re: get return or locals from "exec" str in "environment" lucas <sjlukacs@gmail.com> - 2012-08-30 16:54 -0700
        Re: get return or locals from "exec" str in "environment" lucas <sjlukacs@gmail.com> - 2012-08-30 16:56 -0700
        Re: get return or locals from "exec" str in "environment" lucas <sjlukacs@gmail.com> - 2012-08-30 16:56 -0700
        Re: get return or locals from "exec" str in "environment" Chris Angelico <rosuav@gmail.com> - 2012-08-31 10:13 +1000
      Re: get return or locals from "exec" str in "environment" lucas <sjlukacs@gmail.com> - 2012-08-30 16:54 -0700
  Re: get return or locals from "exec" str in "environment" lucas <sjlukacs@gmail.com> - 2012-08-30 15:25 -0700

csiph-web