Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #17201
| References | (7 earlier) <4EE75382.9060104@sequans.com> <CAN1F8qWRN8_ktQP1WRgPPQaG=mMV=TizcTpoRb=+1RyhznnvOg@mail.gmail.com> <4EE8771D.1050902@sequans.com> <CAPTjJmqiWTvu4pVW7i28KS1bToAnArUj24rMGBkdtzzGJFW=vQ@mail.gmail.com> <4EE890FF.3080708@sequans.com> |
|---|---|
| Date | 2011-12-14 23:21 +1100 |
| Subject | Re: Overriding a global |
| From | Chris Angelico <rosuav@gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.3639.1323865292.27778.python-list@python.org> (permalink) |
On Wed, Dec 14, 2011 at 11:05 PM, Jean-Michel Pichavant
<jeanmichel@sequans.com> wrote:
> Chris Angelico wrote:
>>
>> So... it's a bad idea for me to use 'i' many times in my code, with
>> the same name having different meanings in different places? In
>> languages with infinitely-nesting scopes...
> Bad ideas :
>
> i = 5
>
> def spam():
> for i,v in enumerate([1,2,3,4]):
> for i,v in enumerate(['a','b', 'c']):
> print i, v
> print i,v # bad surprise
That's my point. It's not safe to do it in Python, because the "inner"
local i is the same as the "outer" local i. Python doesn't have block
scope the way most C-like languages do.
int spam()
{
for (int i=0;i<5;++i)
{
for (int i=2;i<4;++i) write("inner "+i+"\n");
write("outer "+i+"\n");
}
}
Works perfectly, and the only cost is that variables must be formally declared.
In Python, you can kinda fudge that sort of thing with nested functions.
def spam():
q=2 # just to prove that the scopes really are nested
for i in range(5):
def ham():
for i in range(2,4):
print("q = %d, i = %d"%(q,i))
ham()
print("i = %d"%i)
It's somewhat clunky, but it does give the illusion of block scope.
Inners mask outers, outers are visible to inner functions. It's an odd
construct though. Very odd.
So... I think I've figured out how to implement from __future__ import braces.
#define { def innerfunc():
#define } innerfunc()
And there you are, out of your difficulty at once!
ChrisA
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Overriding a global Roy Smith <roy@panix.com> - 2011-12-10 15:47 -0500
Re: Overriding a global MRAB <python@mrabarnett.plus.com> - 2011-12-10 21:07 +0000
Re: Overriding a global Roy Smith <roy@panix.com> - 2011-12-10 16:10 -0500
Re: Overriding a global Jean-Michel Pichavant <jeanmichel@sequans.com> - 2011-12-12 12:13 +0100
Re: Overriding a global Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-12-12 21:28 +0000
Re: Overriding a global Dave Angel <d@davea.name> - 2011-12-12 16:43 -0500
Re: Overriding a global Ben Finney <ben+python@benfinney.id.au> - 2011-12-13 09:27 +1100
Re: Overriding a global Dave Angel <d@davea.name> - 2011-12-12 20:46 -0500
Re: Overriding a global Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-12-13 01:48 +0000
Re: Overriding a global Ian Kelly <ian.g.kelly@gmail.com> - 2011-12-12 22:50 -0700
Re: Overriding a global Joshua Landau <joshua.landau.ws@gmail.com> - 2011-12-13 08:34 +0000
Re: Overriding a global Ian Kelly <ian.g.kelly@gmail.com> - 2011-12-13 12:34 -0700
Re: Overriding a global Ian Kelly <ian.g.kelly@gmail.com> - 2011-12-13 12:54 -0700
Re: Overriding a global Jean-Michel Pichavant <jeanmichel@sequans.com> - 2011-12-13 10:54 +0100
Re: Overriding a global Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-12-13 11:15 +0000
Re: Overriding a global Jean-Michel Pichavant <jeanmichel@sequans.com> - 2011-12-13 14:30 +0100
Re: Overriding a global Jean-Michel Pichavant <jeanmichel@sequans.com> - 2011-12-14 11:14 +0100
Re: Overriding a global Chris Angelico <rosuav@gmail.com> - 2011-12-14 21:32 +1100
Re: Overriding a global Jean-Michel Pichavant <jeanmichel@sequans.com> - 2011-12-14 13:05 +0100
Re: Overriding a global Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-12-14 12:53 +0000
Re: Overriding a global Jean-Michel Pichavant <jeanmichel@sequans.com> - 2011-12-14 14:35 +0100
Re: Overriding a global Chris Angelico <rosuav@gmail.com> - 2011-12-14 23:21 +1100
Re: Overriding a global Jean-Michel Pichavant <jeanmichel@sequans.com> - 2011-12-14 18:06 +0100
Re: Overriding a global Terry Reedy <tjreedy@udel.edu> - 2011-12-10 19:14 -0500
Re: Overriding a global Terry Reedy <tjreedy@udel.edu> - 2011-12-10 19:19 -0500
Re: Overriding a global Peter Otten <__peter__@web.de> - 2011-12-11 09:14 +0100
Re: Overriding a global Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2011-12-13 10:15 +0100
csiph-web