[Tutor] Fixing Globals
kendy at kendy.org
kendy at kendy.org
Sat Mar 16 20:04:42 CET 2013
Dear Tutor
Global constants and variables are bad. But what's better? I've heard some
suggestions, but haven't seen much actual code showing how to improve globals. I
don't like:
* Passing a lot of individual arguments.
* Creating a structure with unrelated elements.
* Passing a structure, as an argument, through a function that uses only one (or
none of the) elements in the structure.
I created an example (below), that could be written with global constants and
variables. How would you suggest handling something like this? (I don't need you
to stick to my example.)
#!/usr/bin/python
START = '<'
END = '>'
def getargs():
getops()
if
in_filename_1 =
in_filename_2 =
out_filename_1 =
out_filename_2 =
flag1 =
verbose =
def this():
open_for_read()
stuff()
def open_for_read(filename):
in_filehandle = open(
return in_filehandle
def stuff():
if c == START or c == END:
foo_the_bar =
def that():
things()
write_status(out_filename_1)
def things():
bar_the_foo = foo_the_bar
if verbose:
print(flag1)
def write_status(out_filename_1):
getargs()
this()
that()
I very much appreciate the help that you have given me!
Thanks
Ken
More information about the Tutor
mailing list