[Python-Dev] towards a faster Python

Kevin Jacobs jacobs@penguin.theopalgroup.com
Tue, 10 Jun 2003 10:56:58 -0400 (EDT)


On Tue, 10 Jun 2003, Guido van Rossum wrote:
> > I am talking about what mxTools is doing: adding new builtins to
> > the interpreter by placing them into the __builtins__ dictionary.
> > While I agree that it is usually better to use something like
> > 'from x import *' or even naming the tools explicitly, some features
> > in mxTools do warrant being made builtins, e.g. irange() has been
> > most helpful in the past :-)
> 
> You're not going to convince me to endorse that practice.  End of story.

While not trying to convince you, I do have to say that we use this trick to
provide implementations of builtins to code running under older Python
versions.  This allows us to write code using useful features from Python
2.3 like enumerate, sum, basestring, etc. that can run under Python 2.2. 
For example:

  def export(name, obj):
    import __builtin__
    setattr(__builtin__,name,obj)

  try:
    enumerate

  except NameError:

    def enumerate(l):
      '''Given a sequence l, generates an indexed series:  (0,l[0]), (1,l[1])...'''
      i = 0
      next = iter(l).next
      while 1:
        yield (i, next())
        i += 1

    export('enumerate',enumerate)
 
It isn't the prettiest thing, but it does get the job done.

-Kevin

-- 
--
Kevin Jacobs
The OPAL Group - Enterprise Systems Architect
Voice: (216) 986-0710 x 19         E-mail: jacobs@theopalgroup.com
Fax:   (216) 986-0714              WWW:    http://www.theopalgroup.com