control structures (was "Re: Sins")

skaller skaller at maxtal.com.au
Sun Jan 9 08:39:10 EST 2000


Gordon McMillan wrote:

> > YUKKKKKK!!! There is no natural way to do this.
> > We fall through the loop on failure, instead
> > of success. (using a flag, or testing anything
> > afterwards, is not acceptable as a solution).
> 
> That's the reason that Python has the "for
> .. else" construct that William Tanksley
> hates so much.
> 
>  for i in X:
>    if X[i]==e:
>      # do the "found" thing
>      break
>  else:
>    print "Not Found"

That doesn't work. The 'do the "found" thing'
is in the wrong place: it must be outside the loop,
since it is usually the 'normal continuation'
of the routine.

One solution is delocalisation:

	blah ...
	i = find_in_X()
	blah with i ...

where 'find_in_X()' is a subroutine, and so
the 'do the "found" thing' can be 'return i'.
This sucks: enforced delocalisation is a language design 
fault. 

Languages without lexically scoped anonymous nested function 
expressions_always_ suffer from this problem: C, C++ and Python
included.
Viper partially fixes this, in the Python case, because it provides
lexically scoped nested functions, but there's still no way to
provide anonymous function expressions, (lambda doesn't count)
due to restrictions of the syntax. Interestingly, the latest Java
supports anonymous lexically scoped method expressions.

-- 
John (Max) Skaller, mailto:skaller at maxtal.com.au
10/1 Toxteth Rd Glebe NSW 2037 Australia
homepage: http://www.maxtal.com.au/~skaller
voice: 61-2-9660-0850




More information about the Python-list mailing list