473,326 Members | 2,680 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

The namespace for builtin functions?

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
5 1942
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

88
by: Tim Tyler | last post by:
PHP puts most of its functions into a big flat global namespace. That leads to short function names - but creates a namespace minefield for programmers. Lots of the functions are legacies from...
2
by: Jacek Generowicz | last post by:
Functions defined in Python have type types.FunctionType, and are descriptors whose __get__ method turns them into bound or unbound methods. Functions defined in extension modules have type...
3
by: Gonçalo Rodrigues | last post by:
Hi, Does anyone know if and how I can, from within Python, read the signatures of builtin methods/functions? The following fails: >>> import inspect >>> inspect.getargspec(list.append)...
7
by: Doug Rosser | last post by:
I'm writing a fairly complicated test framework and keeping configuration data inside ini files that are parsed at runtime by the ConfigParser module. For example, there would be a section...
8
by: Elaine Jackson | last post by:
I would like to be able to write a function f, which will live in a module M, and will call a function g, such that, when f is imported from M into the interpreter, and invoked there, its...
11
by: rmm | last post by:
If I replace the open builtin eg import main __main__.__builtins__.open=None Is there any way, from here on, to access the original open function?? Extending this slightly, lets say I put a...
20
by: Ari Krupnik | last post by:
scripts can add methods to the prototypes of builtin objects in JaavScript. I can assign functions to String.prototype.*, for instance. I want to add a method to Node, but when I try to execute...
3
by: bvdp | last post by:
Is it possible to do this from a function: import a module and append the defs in that module to an existing module/namesapce. So, in my code I have something like: # main code import mods ...
17
by: Peng Yu | last post by:
Hi, I'm wondering if there is something in namespace like the 'private' keyword in class? I want to define some class or function that can only be used within that namespace. Thanks, Peng
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.