Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > variables with loop-local scope?

Reply
Thread Tools

variables with loop-local scope?

 
 
Brian Alexander
Guest
Posts: n/a
 
      10-25-2003
I remember using a language many years ago that permitted the creation
of temporary, local variables within the body of a loop (and perhaps
other statement blocks.) Maybe it was Turing.

Can Python do this?

---

EX:
---

for i in listOfNames:
prefixMr = 'Mr'
prefixMs = 'Ms'

if i in mansName:
salutation = prefixMr + i

else:
salutation = prefixMs + i

print 'Hello,' + salutation

----

The idea is that all variables created within the for statements will be
forgotten once the loop exits. No need to exit the procedure.

Is this possible?

Many thanks,

Brian.

 
Reply With Quote
 
 
 
 
Terry Reedy
Guest
Posts: n/a
 
      10-25-2003

"Brian Alexander" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> I remember using a language many years ago that permitted the

creation
> of temporary, local variables within the body of a loop (and perhaps
> other statement blocks.) Maybe it was Turing.
>
> Can Python do this?
> for i in listOfNames:
> prefixMr = 'Mr'
> prefixMs = 'Ms'
> if i in mansName:
> salutation = prefixMr + i
> else:
> salutation = prefixMs + i
> print 'Hello,' + salutation
>
>The idea is that all variables created within the for statements will

be
> forgotten once the loop exits. No need to exit the procedure.


Having the induction variable remain set to its last value is
sometimes useful (and when searching for a match) and almost never a
bother. If and when it is, just delete it.

Terry J. Reedy


 
Reply With Quote
 
 
 
 
Skip Montanaro
Guest
Posts: n/a
 
      10-26-2003
>>>>> "Brian" == Brian Alexander <(E-Mail Removed)> writes:

Brian> Can Python do this?

Sure:

for i in listOfNames:
prefixMr = 'Mr'
prefixMs = 'Ms'
if i in mansName:
salutation = prefixMr + i
else:
salutation = prefixMs + i
else:
del prefixMr
del prefixMs

print 'Hello,' + salutation

Probably not quite what you wanted. Is there a reason you need/want the
variable to disappear before the function returns?

Skip

 
Reply With Quote
 
Alex Martelli
Guest
Posts: n/a
 
      10-26-2003
Brian Alexander wrote:

> I remember using a language many years ago that permitted the creation
> of temporary, local variables within the body of a loop (and perhaps
> other statement blocks.) Maybe it was Turing.


Or C (not that exotic, but does have block-local variables, as do its
descendants).

> Can Python do this?


Python's variables _automatically_ go out of scope only when a function
terminates (you can of course explicitly delete them). So, if you need
"automatic going out of scope", you need a nested function. E.g.:

> for i in listOfNames:
> prefixMr = 'Mr'
> prefixMs = 'Ms'
>
> if i in mansName:
> salutation = prefixMr + i
>
> else:
> salutation = prefixMs + i
>
> print 'Hello,' + salutation


for i in listOfNames:
def block():
prefixMr = 'Mr'
prefixMs = 'Ms'

if i in mansName:
salutation = prefixMr + i
else:
salutation = prefixMs + i
print 'Hello,' + salutation
block()


Alex


 
Reply With Quote
 
Brian Alexander
Guest
Posts: n/a
 
      10-27-2003
Skip Montanaro wrote:
>>>>>>"Brian" == Brian Alexander <(E-Mail Removed)> writes:
>>>>>

>
> Brian> Can Python do this?
>
> Sure:
>
> for i in listOfNames:
> prefixMr = 'Mr'
> prefixMs = 'Ms'
> if i in mansName:
> salutation = prefixMr + i
> else:
> salutation = prefixMs + i
> else:
> del prefixMr
> del prefixMs
>
> print 'Hello,' + salutation
>
> Probably not quite what you wanted. Is there a reason you need/want the
> variable to disappear before the function returns?
>
> Skip
>


Yes, it seems this is one way of doing it. The reason I was interested
is that it can reduce worries in a procedure that may have many local
variables. A large number of locals with very limited (sub-procedural)
scope is better than a large number of locals with procedural scope. (Of
course, having few locals is best of all.)

Brian.

 
Reply With Quote
 
Brian Alexander
Guest
Posts: n/a
 
      10-27-2003
Alex Martelli wrote:
> Brian Alexander wrote:
>
>
>>I remember using a language many years ago that permitted the creation
>>of temporary, local variables within the body of a loop (and perhaps
>>other statement blocks.) Maybe it was Turing.

>
>
> Or C (not that exotic, but does have block-local variables, as do its
> descendants).
>
>
>>Can Python do this?

>
>
> Python's variables _automatically_ go out of scope only when a function
> terminates (you can of course explicitly delete them). So, if you need
> "automatic going out of scope", you need a nested function. E.g.:
>
>
>>for i in listOfNames:
>> prefixMr = 'Mr'
>> prefixMs = 'Ms'
>>
>> if i in mansName:
>> salutation = prefixMr + i
>>
>> else:
>> salutation = prefixMs + i
>>
>> print 'Hello,' + salutation

>
>
> for i in listOfNames:
> def block():
> prefixMr = 'Mr'
> prefixMs = 'Ms'
>
> if i in mansName:
> salutation = prefixMr + i
> else:
> salutation = prefixMs + i
> print 'Hello,' + salutation
> block()
>
>
> Alex
>
>


Alex;

Many thanks. This is quite interesting.

Brian.

 
Reply With Quote
 
Alex Martelli
Guest
Posts: n/a
 
      10-27-2003
Brian Alexander wrote:
...
>> for i in listOfNames:
>> def block():
>> prefixMr = 'Mr'
>> prefixMs = 'Ms'
>>
>> if i in mansName:
>> salutation = prefixMr + i
>> else:
>> salutation = prefixMs + i
>> print 'Hello,' + salutation
>> block()

...
> Many thanks. This is quite interesting.


You're welcome! One important detail: the name 'block' itself
does remain bound (to the function object) even after loop exit;
"del block" after the loop (or in the loop's "else" clause) is
the one way to get rid of it. Performance effects aren't good
at all, either. Consider the following file aa.py:

lotsofnames = ('carlo alex brian joe marco anna rosa kim'.split())*100
masculnames = dict.fromkeys('carlo alex brian joe marco'.split())

def with_block(listOfNames=lotsofnames, mansName=masculnames):
for i in listOfNames:
def block():
prefixMr = 'Mr'
prefixMs = 'Ms'

if i in mansName:
salutation = prefixMr + i
else:
salutation = prefixMs + i
#print 'Hello,' + salutation
block()

def without_block(listOfNames=lotsofnames, mansName=masculnames):
for i in listOfNames:
prefixMr = 'Mr'
prefixMs = 'Ms'

if i in mansName:
salutation = prefixMr + i
else:
salutation = prefixMs + i
#print 'Hello,' + salutation

and the measurements:

[alex@lancelot bo]$ timeit.py -c -s'import aa' 'aa.with_block()'
100 loops, best of 3: 2.1e+03 usec per loop

[alex@lancelot bo]$ timeit.py -c -s'import aa' 'aa.without_block()'
1000 loops, best of 3: 630 usec per loop

so, we're slowing the function down by over 3 times by repeating
the 'def' (and using nonlocal access for 'i' and 'mansName' in
the block, but that's minor, as can be verified by injecting those
names as locals via the "default arguments" idiom). Thus, one
rarely puts a 'def' (or, for that matter, a 'class') inside a
loop -- the repetition really buys you nothing much, after all.


Alex

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
what is difference between Class variables and Instance variables? rahul8143@gmail.com Java 10 06-06-2011 06:43 AM
Put variables into member variables or function variables? tjumail@gmail.com C++ 9 03-23-2008 04:03 PM
Session Variables and Static Variables cobus.lombard@gmail.com ASP .Net 1 03-26-2006 11:05 AM
Accessing class member variables - properties or variables? dwok Java 7 03-04-2005 03:54 AM
Re: Class public shared Variables vs. Application Variables in ASP.NET avnrao ASP .Net 0 05-07-2004 05:28 AM



Advertisments