variables with loop-local scope?

Brian Alexander brian094 at sympatico.ca
Sat Oct 25 18:02:22 EDT 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.





More information about the Python-list mailing list