Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #16798
| Path | csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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; 'one?': 0.05; 'run-time': 0.05; 'function,': 0.07; 'prints': 0.07; 'python': 0.08; '(those': 0.09; 'function),': 0.09; 'says,': 0.09; 'subject:string': 0.09; 'variables,': 0.09; 'am,': 0.12; 'def': 0.13; 'received:209.85.210.174': 0.13; 'received:mail- iy0-f174.google.com': 0.13; "'a'": 0.16; 'a,b,c': 0.16; 'behaviour,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; "function's": 0.16; 'namespace.': 0.16; 'op:': 0.16; 'scope.': 0.16; 'subject:creation': 0.16; 'subject:variable': 0.16; 'this:': 0.16; 'meant': 0.17; 'language,': 0.17; 'wrote:': 0.18; 'dec': 0.22; 'assume': 0.22; 'header:In-Reply-To:1': 0.22; 'works.': 0.23; 'defined': 0.24; 'function': 0.27; '(and': 0.28; '(this': 0.28; 'message- id:@mail.gmail.com': 0.28; 'pass': 0.29; 'second': 0.29; 'outer': 0.30; 'usually': 0.31; 'quite': 0.32; 'thu,': 0.32; 'actual': 0.32; 'actually': 0.33; 'to:addr:python-list': 0.34; 'function.': 0.34; 'nested': 0.34; 'variables': 0.37; 'but': 0.37; 'received:google.com': 0.37; "there's": 0.37; 'another': 0.37; 'not,': 0.37; 'steven': 0.38; 'received:209.85': 0.38; 'subject:from': 0.38; 'called': 0.40; "it's": 0.40; 'received:209': 0.40; 'to:addr:python.org': 0.40; 'one,': 0.40; '2011': 0.61; 'share': 0.66; 'shares': 0.67; 'sum': 0.89 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=FLOGH1wTJFRy3sE3J7AHm8QpOlakbIS7gg7t2U9ew7Y=; b=peSfwXItiCyv/788n044RbIyM+zpZPthVyehCcYrWXXxFBj69XXDKmnO4lFrOTb9tP 8Yv/+EDXrU9OTWtn0sM+XYwZRrTqZ0BZ/c2oBignxHrYmEf5Mc5SMTjnJMtAufMBSZ86 ztmED9i7lytzvTquJku8nZ7Jw4lSiYtW8/CKw= |
| MIME-Version | 1.0 |
| In-Reply-To | <4edffed8$0$29988$c3e8da3$5496439d@news.astraweb.com> |
| References | <b078a04b-024b-48dc-b24a-8f4ce75fa238@13g2000vbu.googlegroups.com> <4edffed8$0$29988$c3e8da3$5496439d@news.astraweb.com> |
| Date | Thu, 8 Dec 2011 14:13:19 +1100 |
| Subject | Re: Dynamic variable creation from string |
| 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.3390.1323314002.27778.python-list@python.org> (permalink) |
| Lines | 45 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1323314002 news.xs4all.nl 6843 [2001:888:2000:d::a6]:35928 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | x330-a1.tempe.blueboxinc.net comp.lang.python:16798 |
Show key headers only | View raw
On Thu, Dec 8, 2011 at 11:03 AM, Steven D'Aprano
<steve+comp.lang.python@pearwood.info> wrote:
>> It is really important that the scope of a,b,c is limited to the Sum
>> function, they must not exisit outside it or inside any other nested
>> functions.
>
> The second part is impossible, because that is not how Python works.
> Nested functions in Python can always see variables in their enclosing
> scope. If you don't want that behaviour, use another language, or don't
> use nested functions.
To the OP: By "nested functions", did you mean actual nested functions
(those defined inside this function), or simply functions called from
this one?
This is a nested function, as the term is usually taken to mean:
def outer_function():
a = 1
def inner_function():
b = 2
return a+b
print(inner_function()) # Prints 3
The inner function has access to all of the outer function's
namespace. But if you meant this:
def function_1():
b = 2
return a+b
def function_2():
a = 1
print(function_1())
then it's quite the other way around - Python never shares variables
in this way (this would have function_1 assume that 'a' is a global
name, so if it's not, you'll get a run-time NameError).
As Steven says, there's no way in Python to hide variables from an
actual nested function. But if you just mean a function called from
this one, then what you want is the normal behaviour (and if you
actually want to share variables, you pass them as arguments).
ChrisA
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Dynamic variable creation from string Massi <massi_srb@msn.com> - 2011-12-07 09:09 -0800
Re: Dynamic variable creation from string John Gordon <gordon@panix.com> - 2011-12-07 17:45 +0000
Re: Dynamic variable creation from string MRAB <python@mrabarnett.plus.com> - 2011-12-07 18:07 +0000
Re: Dynamic variable creation from string "Waldek M." <wm@localhost.localdomain> - 2011-12-07 18:46 +0100
Re: Dynamic variable creation from string Chris Angelico <rosuav@gmail.com> - 2011-12-08 04:52 +1100
Re: Dynamic variable creation from string Terry Reedy <tjreedy@udel.edu> - 2011-12-07 17:59 -0500
Re: Dynamic variable creation from string Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-12-08 00:03 +0000
Re: Dynamic variable creation from string Terry Reedy <tjreedy@udel.edu> - 2011-12-07 19:27 -0500
Re: Dynamic variable creation from string Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-12-08 01:59 +0000
Re: Dynamic variable creation from string Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2011-12-08 10:54 +0200
Re: Dynamic variable creation from string Massi <massi_srb@msn.com> - 2011-12-09 01:55 -0800
Re: Dynamic variable creation from string Peter Otten <__peter__@web.de> - 2011-12-09 12:27 +0100
Re: Dynamic variable creation from string Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-12-09 11:59 +0000
Re: Dynamic variable creation from string Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-12-09 12:03 +0000
Re: Dynamic variable creation from string Chris Angelico <rosuav@gmail.com> - 2011-12-09 23:08 +1100
Re: Dynamic variable creation from string Ethan Furman <ethan@stoneleaf.us> - 2011-12-09 12:01 -0800
Re: Dynamic variable creation from string Nobody <nobody@nowhere.com> - 2011-12-11 06:42 +0000
Re: Dynamic variable creation from string alex23 <wuwei23@gmail.com> - 2011-12-11 19:31 -0800
Re: Dynamic variable creation from string Ethan Furman <ethan@stoneleaf.us> - 2011-12-11 21:00 -0800
Re: Dynamic variable creation from string alex23 <wuwei23@gmail.com> - 2011-12-11 22:50 -0800
Re: Dynamic variable creation from string Chris Angelico <rosuav@gmail.com> - 2011-12-08 14:13 +1100
Re: Dynamic variable creation from string alex23 <wuwei23@gmail.com> - 2011-12-11 23:11 -0800
Re: Dynamic variable creation from string 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-12 04:43 -0800
Re: Dynamic variable creation from string 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-12 04:49 -0800
Re: Dynamic variable creation from string alex23 <wuwei23@gmail.com> - 2011-12-12 15:45 -0800
Re: Dynamic variable creation from string Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-12-13 01:35 +0000
Re: Dynamic variable creation from string 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-13 06:21 -0800
csiph-web