tags:

views:

121

answers:

2

How do you explicitly access name in Python's built in scope?

One situation where I ran in to this was a in module, say called foo, which happened to have an open function. In another module foo's open function would be accessible as foo.open which works well. In foo itself though, open blocks the built in open. How can you access the built in version of a name like open explicitly?

I am aware it is probably practically bad idea to block any built in name, but I am still curious to know if there is a way to explicitly access the built in scope.

A: 

It's something like

__builtins__.open()
David Zaslavsky
__builtins__ is an implementation detail of CPython. I wouldn't rely on it.
Chris B.
True... I was originally going to suggest __builtin__ but I'd forgotten that it was a module :-/
David Zaslavsky
+6  A: 

Use __builtin__.

def open():
    pass

import __builtin__

print open
print __builtin__.open

... gives you ...

<function open at 0x011E8670>
<built-in function open>

Chris B.
Bonus point if someone can tell me when this was first included in python. The docs are normally good at saying that, but it doesn't seem to be the case here: http://docs.python.org/library/__builtin__.html
amjoconn
A long, long time as you can see this thread dating from 1992: http://www.python.org/search/hypermail/python-1992/0049.html (it was called builtin instead of __builtin__ back then). Would not worry about older versions not supporting it ;-)
ChristopheD