__builtins_ weirdness
Tim Peters
tim_one at email.msn.com
Thu Dec 2 03:10:37 EST 1999
[Juergen A. Erhard]
> 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 only wish there would be some consistency here. Or some *very* good
> explanation.
As /F said, you're mucking with internals here. You can find very good
explanations in DejaNews if you're determined, but better to just begin your
module with
_range = range
and then refer to _range inside your function. That is, you're mucking with
internals when there's no need to muck with internals at all -- indeed,
when mucking with internals is much slower and clumsier than doing the
obvious <wink> thing above.
> def range(*args):
> ...
Redefining builtin names is dubious practice. Name it myrange() (or
something), and you wouldn't have to worry about getting back the builtin
range -- and your code readers wouldn't have to scratch their heads
wondering what the heck e.g. range(list) means (virtually all Python
programmers would see that and be certain the code was broken).
> ...
> But in a module, __builtins__ `magically' transforms into a dict...
Section 4.1 ("Code blocks, execution frames, and namespaces") of the Lang
Ref explains part of that.
__builtins__-is-not-a-module-but-__builtin__-is-ly y'rs - tim
More information about the Python-list
mailing list