global variables or inheritence
Donn Cave
donn at u.washington.edu
Wed Apr 26 18:03:52 EDT 2000
Quoth Thomas Rasmussen <simpsons at kom.auc.dk>:
...
| Well i'm programming a small admin tool, which must use the users
| $EDITOR for editing files. This works fine if the EDITOR sysenv is
| set, if not, then i want to ask the user which editor he want to use,
| and then pass this variable to the module that handles
| filemanipulation. This modules is also in another file if it
| matters... I must add that this is my first real program made in
| python...
When things get difficult in Python, my reflex is to write a
big hairy class. Maybe something like this would work, or at
least give you an idea:
import os
import sys
class UserEditor:
def __init__(self, default_editor = None):
self.program = default_editor
def get(self):
if not self.program:
try:
self.program = os.environ['EDITOR']
except KeyError:
sys.stdout.write('Editor? ')
... etc.
return self.program
def set(self, editor):
self.program = editor
def invoke(self, file):
program = self.get()
return os.system('%s %s' % (program, file)) == 0
editor = UserEditor()
Let's say that's at module scope in the "interfaces" module. In other
modules you may say
filemanipulation.xyzfile(interfaces.editor.get(), ...)
or maybe this class will turn out to be a convenient place to do some
stuff that is now cluttering up filemanipulation, so you should say
filemanipulation.xyzfile(interfaces.editor, ...)
Donn Cave, University Computing Services, University of Washington
donn at u.washington.edu
More information about the Python-list
mailing list