Connecting Tech Pros Worldwide Forums | Help | Site Map

The namespace for builtin functions?

Blair Hall
Guest
 
Posts: n/a
#1: Jul 18 '05
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)

#-------------------------------------
if(__name__ == '__main__'):

print "Three = ",apply(lambda x,y: x+y, (1,2) )


This seems to work, but if I import the definition of 'apply', like:
[color=blue][color=green][color=darkred]
>>> from myApply import apply
>>> apply(lambda x,y: x+y, (1,2) )[/color][/color][/color]

I get a crash:

Traceback (most recent call last):
File "<interactive input>", line 1, in ?
File "C:\proj_py\Learning\builtins\myApply.py", line 5, in apply
return __builtins__.apply(func,seq)
AttributeError: 'dict' object has no attribute 'apply'

I can't see what to use instead of '__builtins__' as the
namespace for the built in functions.


Jay O'Connor
Guest
 
Posts: n/a
#2: Jul 18 '05

re: The namespace for builtin functions?


Blair Hall wrote:
[color=blue]
> 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?[/color]



Save yourself a lot of trouble, just give it a different name.

Peter Hansen
Guest
 
Posts: n/a
#3: Jul 18 '05

re: The namespace for builtin functions?


Blair Hall wrote:[color=blue]
>
> 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)[/color]

Try this instead:

_builtin_apply = apply

def apply(func, seq):
return _builtin_apply(func, seq)

But as Jay says, you're probably better off just not using
the same name...

-Peter
Francis Avila
Guest
 
Posts: n/a
#4: Jul 18 '05

re: The namespace for builtin functions?


Jay O'Connor wrote in message ...[color=blue]
>Blair Hall wrote:
>[color=green]
>> 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?[/color]
>
>
>
>Save yourself a lot of trouble, just give it a different name.
>[/color]

This is the best advice.

However...

I don't understand why in __main__, the name __builtins__ refers to the
module object __builtin__, but in any other namespace, __builtins__ is the
__dict__ of __builtin__!

E.g.:
--- a.py ---
a = lambda: __builtins__
--- END ---
[color=blue][color=green][color=darkred]
>>> __builtins__[/color][/color][/color]
<module '__builtin__' (built-in)>[color=blue][color=green][color=darkred]
>>> id(__builtins__)[/color][/color][/color]
6577648[color=blue][color=green][color=darkred]
>>> id(__builtins__.__dict__)[/color][/color][/color]
6584048[color=blue][color=green][color=darkred]
>>> import a
>>> id(a.a())[/color][/color][/color]
6584048

Why not 6577648?!
--
Francis Avila

Fredrik Lundh
Guest
 
Posts: n/a
#5: Jul 18 '05

re: The namespace for builtin functions?


Blair Hall wrote:
[color=blue]
> 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)[/color]

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

</F>




Bengt Richter
Guest
 
Posts: n/a
#6: Jul 18 '05

re: The namespace for builtin functions?


On Sun, 30 Nov 2003 19:00:16 +0100, "Fredrik Lundh" <fredrik@pythonware.com> wrote:
[color=blue]
>Blair Hall wrote:
>[color=green]
>> 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)[/color]
>
>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[/color]
Weird -- netscape 4.5 claims that document "contains no data"
wget got it though. Maybe time to reboot windows ;-/[color=blue]
>[/color]
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):
[color=blue][color=green][color=darkred]
>>> file('empty.py').read()[/color][/color][/color]
'\n'[color=blue][color=green][color=darkred]
>>> import empty
>>> dir(empty)[/color][/color][/color]
['__builtins__', '__doc__', '__file__', '__name__']

Ok, now in the interactive namespace:
[color=blue][color=green][color=darkred]
>>> __builtins__[/color][/color][/color]
<module '__builtin__' (built-in)>

And in the 'empty' module's namespace:
[color=blue][color=green][color=darkred]
>>> `empty.__builtins__`[:60][/color][/color][/color]
"{'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__ ;-)
[color=blue][color=green][color=darkred]
>>> type(__builtins__)[/color][/color][/color]
<type 'module'>[color=blue][color=green][color=darkred]
>>> type(empty.__builtins__)[/color][/color][/color]
<type 'dict'>[color=blue][color=green][color=darkred]
>>> __builtins__.__dict__ is empty.__builtins__[/color][/color][/color]
True[color=blue][color=green][color=darkred]
>>>[/color][/color][/color]

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
Closed Thread