Py3K: True and False builtin or keyword?
Regarding the discussion about None becoming a keyword in Py3K: Recently the truth values True and False have been mentioned. Should they become builtin values --like None is now-- or should they become keywords? Nevertheless: for the time being I came up with the following weird idea: If you put this in front of the main module of a Python app: #!/usr/bin/env python if __name__ == "__main__": import sys if sys.version[0] <= '1': __builtins__.True = 1 __builtins__.False = 0 del sys # --- continue with your app from here: --- import foo, bar, ... .... Now you can start to use False and True in any immported module as if they were already builtins. Of course this is no surprise here and Python is really fun, Peter.
"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
participants (2)
-
Barry A. Warsaw -
pf@artcom-gmbh.de