Conditional Expressions don't solve the problem

Dale Strickland-Clark dale at riverhall.NOSPAMco.uk
Tue Oct 16 09:38:21 EDT 2001


I'm a bit mystified by all the activitiy on conditional expressions
that appears to have originated from a post a while ago about curious
assignment behaviour.

Have I missed something here (quite possible) or are the two issues
related? Because it doesn't seem so to me.

The original discussion went on to talk about the loop and a half
problem which sumarised it quite nicely for me.

Take this short extract from some code I'm working on at the moment:

term = self.nextTerm()
while term:
    handled = 0
    for a in argObjects: # Loop through all the type Handlers.
        if a.test(term): # Pass arg to handler.
            handled = 1
            break        # If true, handler has dealt with it.
    if not handled:
        raise whatever
    term = self.nextTerm()

I think this is verbose. I would much prefer be able to write:

while term := self.nextTerm():
    for a in argObjects:
        if handled := a.test(term):
             break
    if not handled:
        raise whatever

Choose your own asignment operator.

How does the if/then/else construct help me here?


--
Dale Strickland-Clark
Riverhall Systems Ltd



More information about the Python-list mailing list