Re: Accessing overridden __builtin__s?



garyjefferson123@xxxxxxxxx wrote:
I'm having a scoping problem. I have a module called SpecialFile,

The convention is to use all_lowercase names for modules, and CamelCase
for classes.

which defines:

def open(fname, mode):
return SpecialFile(fname, mode)

This shadows the builtin open() function.

class SpecialFile:

Old-style classes are deprecated, please use new-style classes

def __init__(self, fname, mode):
self.f = open(fname, mode)
...


The problem, if it isn't obvioius, is that the open() call in __init__
no longer refers to the builtin open(), but to the module open(). So,
if I do:

f = SpecialFile.open(name, mode)

I get infinite recursion.

How do I tell my class that I want to refer to the __builtin__ open(),
and not the one defined in the module?

You can use Steven's solution, or keep a reference to the builtin open()
function before defining your own open() function:

builtin_open = open
def open(fname, mode):
return SpecialFile(fname, mode):

class SpecialFile(object):
def __init__(self, fname, mode):
self.f = builtin_open(fname, mode)



--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'onurb@xxxxxxxxxxx'.split('@')])"
.



Relevant Pages