The namespace for builtin functions?

B

Blair Hall

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:

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.
 
J

Jay O'Connor

Blair said:
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?



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

Peter Hansen

Blair said:
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)

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
 
F

Francis Avila

Jay O'Connor wrote in message ...
Save yourself a lot of trouble, just give it a different name.

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 ---
6584048

Why not 6577648?!
 
F

Fredrik Lundh

Blair said:
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

</F>
 
B

Bengt Richter

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):
['__builtins__', '__doc__', '__file__', '__name__']

Ok, now in the interactive namespace:
<module '__builtin__' (built-in)>

And in the 'empty' module's namespace:
"{'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__ ;-)

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top