variables with loop-local scope?

Brian Alexander brian094 at sympatico.ca
Sun Oct 26 21:48:01 EST 2003


Skip Montanaro wrote:
>>>>>>"Brian" == Brian Alexander <brian094 at sympatico.ca> 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.





More information about the Python-list mailing list