By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,973 Members | 1,854 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,973 IT Pros & Developers. It's quick & easy.

The namespace for builtin functions?

P: n/a
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:
from myApply import apply
apply(lambda x,y: x+y, (1,2) )


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.

Jul 18 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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?


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

Jul 18 '05 #2

P: n/a
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)


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
Jul 18 '05 #3

P: n/a
Jay O'Connor wrote in message ...
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?


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 ---
__builtins__ <module '__builtin__' (built-in)> id(__builtins__) 6577648 id(__builtins__.__dict__) 6584048 import a
id(a.a())

6584048

Why not 6577648?!
--
Francis Avila

Jul 18 '05 #4

P: n/a
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

</F>


Jul 18 '05 #5

P: n/a
On Sun, 30 Nov 2003 19:00:16 +0100, "Fredrik Lundh" <fr*****@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
Jul 18 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.