472,954 Members | 1,929 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,954 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 1928
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: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

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.