__builtins_ weirdness

Juergen A. Erhard jae at ilk.de
Wed Dec 1 18:30:48 EST 1999


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

This really weirded me out:

  __builtins__.range and __builtins["range"] both are the `range'
  function, one only works in interactive mode, and the other only in
  a module.

I found this while I wrote this little helper fn:

  def range(*args):
      if len(args)==1 and type(args[0])==type([]):
          return apply(__builtins__["range"], (len(args[0]),))
      elif len(args)==1 and type(args[0])==types.InstanceType:
          return args[0].__range__()
      else:
          return apply(__builtins__["range"], args)

(should be the real def of `range', obviously ;-)

Now, I twiddled with this in the interactive interpreter, where the
obvious __builtins__.range works (with __name__=="__main__").

But in a module, __builtins__ `magically' transforms into a dict...

I only wish there would be some consistency here.  Or some *very* good
explanation.

Bye, J

PS: The above snippet is under GPL ;-) (at least, the module it's in
is... the fn itself is obviously to small to be copyrighted).

PPS: That __range__ method is something I `invented' so this range fn
can also be applied to user-defined objects.

- -- 
Jürgen A. Erhard      eMail: jae at ilk.de      phone: (GERMANY) 0721 27326
         MARS: http://Juergen_Erhard.tripod.com/mars_index.html
                Debian GNU/Linux (http://www.debian.org)
  "Windows NT" is an acronym for "Windows? No thanks." -- Russ McManus
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.0 (GNU/Linux)
Comment: Processed by Mailcrypt 3.5.5 and Gnu Privacy Guard <http://www.gnupg.org/>

iEYEARECAAYFAjhFr6YACgkQN0B+CS56qs02vACcDYeC9IgFhe8QqYV5ke74K0bl
0cwAoJAyKVIF8suZkofaKm5X1/yYZPa0
=oUlC
-----END PGP SIGNATURE-----




More information about the Python-list mailing list