The namespace for builtin functions?
Bengt Richter
bokr at oz.net
Sun Nov 30 17:49:29 EST 2003
On Sun, 30 Nov 2003 19:00:16 +0100, "Fredrik Lundh" <fredrik at pythonware.com> wrote:
>Blair Hall wrote:
>
>> Can anyone please tell me how to correctly use a built in function
>> when there is a function of the same name in local scope?
>>
>> Here is an example. Suppose the following is in myApply.py:
>>
>> def apply(func,seq):
>> #
>> # Code can default to
>> # built-in definition in some cases:
>> return __builtins__.apply(func,seq)
>
>the module is named __builtin__, and must be imported before
>it can be used.
>
>__builtins__ is a CPython implementation detail (it's used to cache
>a reference to the builtin modules, and are initialized on demand).
>
>for more info, see the "Overloading functions from the __builtin__
>module" here:
>
> http://effbot.org/zone/librarybook-builtin.htm
Weird -- netscape 4.5 claims that document "contains no data"
wget got it though. Maybe time to reboot windows ;-/
>
I think I agree with Francis. Why is __builtins__ set up
in the interactive namespace differently from an imported
module's namespace? To show what he was saying again, I made
and empty module (nothing but an empty line in its source):
>>> file('empty.py').read()
'\n'
>>> import empty
>>> dir(empty)
['__builtins__', '__doc__', '__file__', '__name__']
Ok, now in the interactive namespace:
>>> __builtins__
<module '__builtin__' (built-in)>
And in the 'empty' module's namespace:
>>> `empty.__builtins__`[:60]
"{'help': Type help() for interactive help, or help(object) f"
(I knew I would be getting the whole dict repr if I just typed empty.__builtins__ ;-)
>>> type(__builtins__)
<type 'module'>
>>> type(empty.__builtins__)
<type 'dict'>
>>> __builtins__.__dict__ is empty.__builtins__
True
>>>
Seems inconsistent. Why not
__builtins__ is empty.__builtins__ => True
and hence
__builtins__.__dict__ is empty.__builtins__.__dict__ => True
(or maybe both __builtins__ bindings could be to the dict, though that would
bypass potential getattr magic that might be needed somewhere?)
Regards,
Bengt Richter
More information about the Python-list
mailing list