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"

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'python,': 0.02; 'output': 0.04; '"""': 0.05; 'exec': 0.07; 'executable': 0.07; 'executed': 0.07; 'function,': 0.07; 'python': 0.09; 'itself,': 0.09; 'def': 0.10; 'aug': 0.13; 'value.': 0.15; 'called,': 0.16; 'env,': 0.16; 'executed.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; "function's": 0.16; 'function;': 0.16; 'function?': 0.16; 'integers;': 0.16; 'rtn': 0.16; 'statement.': 0.16; 'syntax,': 0.16; 'wrote:': 0.17; 'creates': 0.18; '>>>': 0.18; 'equivalent': 0.20; 'putting': 0.20; 'received:209.85.214.174': 0.21; '(usually': 0.22; '31,': 0.22; 'for?': 0.23; 'statement': 0.23; 'this:': 0.23; '(this': 0.24; 'command': 0.24; 'pass': 0.25; 'header:In-Reply-To:1': 0.25; 'am,': 0.27; 'see,': 0.27; 'message-id:@mail.gmail.com': 0.27; "doesn't": 0.28; 'post': 0.28; 'mirror': 0.29; 'strings,': 0.29; 'objects': 0.29; 'included': 0.29; 'that.': 0.30; 'fri,': 0.30; 'returned': 0.30; 'function': 0.30; 'code': 0.31; 'function.': 0.33; 'harry': 0.33; 'to:addr:python-list': 0.33; 'received:google.com': 0.34; 'screen': 0.34; 'mapping': 0.35; "won't": 0.35; 'received:209.85': 0.35; 'but': 0.36; 'subject:" ': 0.36; 'test': 0.36; 'received:209': 0.37; 'far': 0.37; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'received:209.85.214': 0.39; 'called': 0.39; 'header:Received:5': 0.40; 'your': 0.60; "you'll": 0.62; 'show': 0.63; 'soon': 0.70; 'subject:get': 0.81; "'foo'": 0.84; 'anywhere.': 0.84; 'around,': 0.84; 'construct': 0.84; 'locals': 0.84; 'object:': 0.84; 'lucas': 0.93
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=F9y3T8ml0e2Rn8jTAm08oCJitA/5Ejgv85QHbQJr1T8=; b=aB6efs2wdDdjwLUFkMoqAVv72LR7WgunGhEpUtipG4ObGO11f4+QSkypzRjwlOxbm6 IXu50IshoRHPaRMT+QbgkKyQwRORswQdbECJNLVTsyUnUOMz8UqDfKv+O7wB0RRYElzJ gVhQimuifcL7xM2xATipphVxRkyGRYMqmf6x+9Nx7eH7h1c9mZ9Q7/j2vsZGvPDp44J/ JntEm59T4C55BEx6kHB2GP8u6xwyd5T6U7704G6nZUHN5bKcv2xkrpPXMUKVFmsuk2/H KucDyeo7W8JzmOer3HesKCNML5rPpUXbWwt9dfrGhVcLhsShqK0FOsJBeCLri1NTh02c NXxw==
MIME-Version 1.0
In-Reply-To <599e5709-22ab-4e85-8443-181a2ae19c17@googlegroups.com>
References <5c488ca5-d730-40c4-b860-7a53201f21ae@googlegroups.com> <mailman.3987.1346365155.4697.python-list@python.org> <599e5709-22ab-4e85-8443-181a2ae19c17@googlegroups.com>
Date Fri, 31 Aug 2012 08:45:14 +1000
Subject Re: get return or locals from "exec" str in "environment"
From Chris Angelico <rosuav@gmail.com>
To python-list@python.org
Content-Type text/plain; charset=ISO-8859-1
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.3991.1346366717.4697.python-list@python.org> (permalink)
Lines 49
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1346366717 news.xs4all.nl 6872 [2001:888:2000:d::a6]:53846
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:28147

Show key headers only | 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