[Python-Dev] Py3K: True and False builtin or keyword?

Barry A. Warsaw bwarsaw@cnri.reston.va.us
Thu, 23 Mar 2000 18:19:28 -0500 (EST)


>>>>> "PF" == Peter Funk <pf@artcom-gmbh.de> writes:

    PF> Now you can start to use False and True in any immported
    PF> module as if they were already builtins.  Of course this is no
    PF> surprise here and Python is really fun, Peter.

You /can/ do this, but that doesn't mean you /should/ :) Mucking with
builtins is fun the way huffing dry erase markers is fun.  Things are
very pretty at first, but eventually the brain cell lossage will more
than outweigh that cheap thrill.

I've seen a few legitimate uses for hacking builtins.  In Zope, I
believe Jim hacks get_transaction() or somesuch into builtins because
that way it's easy to get at without passing it through the call tree.
And in Zope it makes sense since this is a fancy database application
and your current transaction is a central concept.

I've occasionally wrapped an existing builtin because I needed to
extend it's functionality while keeping it's semantics and API
unchanged.  An example of this was my pre-Python-1.5.2 open_ex() in
Mailman's CGI driver script.  Before builtin open() would print the
failing file name, my open_ex() -- shown below -- would hack that into
the exception object.

But one of the things about Python that I /really/ like is that YOU
KNOW WHERE THINGS COME FROM.  If I suddenly start seeing True and
False in your code, I'm going to look for function locals and args,
then module globals, then from ... import *'s.  If I don't see it in
any of those, I'm going to put down my dry erase markers, look again,
and then utter a loud "huh?" :)

-Barry

realopen = open
def open_ex(filename, mode='r', bufsize=-1, realopen=realopen):
    from Mailman.Utils import reraise
    try:
        return realopen(filename, mode, bufsize)
    except IOError, e:
        strerror = e.strerror + ': ' + filename
        e.strerror = strerror
        e.filename = filename
        e.args = (e.args[0], strerror)
        reraise(e)

import __builtin__
__builtin__.__dict__['open'] = open_ex