network programming without goto

Caleb Hattingh caleb1 at telkomsa.net
Thu Nov 25 20:59:54 EST 2004


On Thu, 25 Nov 2004 06:53:53 -0800 (PST), kent sin <kentsin at yahoo.com>  
wrote:
>
> for host in hostlist:
>     try:
>         # sometimes the host may fail
>         conn =zoom.Connecton(host.ip, host.port)
>     except:
>         continue  # Next host? But How to give it some
> chance? 3 times?
>     ...

Roy really posted the definitive answer here, but there is something else  
you may find it interesting to try: recursive functions.  I played around  
with this for a random POV-Ray texture generator I made a while ago  
(anyone interested?  It's decent :) - nesting levels were somewhat  
flexible, and I made the number of nesting levels in different sections  
(texture maps, normal maps, etc.) randomised as well.

You do something like

***
def funnyWalk(stuff, NUMBER_OF_THIS_TRY, NESTING_LEVEL)
     if NUMBER_OF_THIS_TRY > NESTING_LEVEL:
         # We're done.
         break # Is this valid??? I confuse pascal and python easily :(
     NailParrotsFeet()
     if NEED_ANOTHER_CALL == true:
         funnyWalk(stuff,NUMBER_OF_THIS_TRY+1)

NESTINGLEVEL = 3
funnyWalk(stuff,1,NESTINGLEVEL)
***

And "NailParrotsFeet()" function will get run 3 times.




More information about the Python-list mailing list