[Tutor] Better structure?

Alan Gauld alan.gauld at freenet.co.uk
Mon Jan 31 19:51:56 CET 2005


> def start():
      .... lots of lines...
>     global xaxis
>     global yaxis

Its traditional to put global statements at the top of the function.
Also you only need one line to list all of the global variables

>     global radiusaxis
>     global radiusaxis2

Similarly here., and again you can put all four in one place.

>     radiusaxis2.visible = 0

And since there is no input parameter and no return statement
and you only call start() once...

> start()

Why bother with defining a function? Just put all the code 
inline and omit the globals and it will be the same...

but better is probably to jkeep the function and use

if __name__ == '__main__; start()

> def graphit(type,f,range2,step):
>     li = curve(color=color.blue)
>     if type in ['y','x']:
>         x = -15
>         radiusaxis.visible = 0
>         radiusaxis2.visible = 0
>         while -15 <= x <= 15:
>             if eval(range2):
>                 try:
>                     a = f(x)
>                     if -15 <= a <= 15:
>                         if type == 'y':
>                             li.append(pos=(x,a,0))
>                         elif type == 'x':
>                             li.append(pos=(a,x,0))
>                     else:
>                         li = curve(color=color.blue)
>                 except:
>                     pass

This is iffy. If *anthing* happens you ignore it.
Now suppose due to a bug you go into an infinite 
loop and you try to stop using CTRL-C -  it won't work!

If you must use a catch-all except then do something 
with it - print a message at least!

I got a sore head after that... I'll leave the rest for 
others to remark upon. :-)

Alan G.


More information about the Tutor mailing list