In defence of the two-namespace rule

Edward Welbourne eddyw at lsl.co.uk
Fri Jan 21 12:14:58 EST 2000


Haskell B. Currie was a mathematician who made the simple observation
that a function of many arguments may be described systematically as a
function taking one argument, the return from which is in turn a
function taking the next argument, and so on until you get a function
which takes the last argument and returns the answer you wanted.
This is a great simplifier for mathematicians discussing algebra.

In computing, his first name graces an excellent `functional
programming' language - which, unlike some of its kin is also a
functional `programming language' - and his second name is used to
describe the operation (e.g. binding a method to an instance) of
combining a function and a value to form a function which takes one
argument fewer than the other, using the value just given as the first
argument, so as to make up the missing one.

And while I grant the os.getcwd() one may be dopey, there are places
where a user interface specifies that `if the user supplies a relative
path for this input, it will be interpreted as a path relative to
/blah/stuff/' in which one may wish to supply a tool-function to the
dialog through which the user provides their input - the tool-function
has the job of resolving this spec - so use '/blah/stuff' in place of
os.getcwd(); more likely, use os.environ.get('blahrc', '/blah/stuff').
Since the dialog is going to call this function quite a lot (e.g. it's a
file selection dialog, so it's going to allow directory-selection on a
widget and build lists of things the user might call, etc.), we don't
want to recompute blahrc, so we do it once.

As to passing the default through globals, the problem then gets
complicated by the fact that, of course, that dialog gets used by loads
of bits of code, so someone writes this helper:

def relativeto(root):
    def result(*fragments, r=root):
        return apply(os.path.join, (r,) + fragments)
    return result

which doesn't have the option of passing root through globals.  Either
you need to transcribe it, as shown, or you need to remember the
namespace relativeto() used in each of its runs for as long as you kept
its result.  Not a big problem with relativeto, but then if I go giving
an example in which you would really have a huge mess on your hands, the
example is going to be culled from some general-purpose context and folk
are going to complain that they can't follow what's going on.  Examples
depend on the reader imagining how much more complex the situation might
be in real-world code instead of nice easy-to-follow code.

	Eddy.
--
Uh-oh - pervect alert ;^>




More information about the Python-list mailing list