Re: Accessing overridden __builtin__s?
- From: bruno at modulix <onurb@xxxxxxxxxxx>
- Date: Tue, 14 Mar 2006 10:27:06 +0100
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('@')])"
.
- References:
- Accessing overridden __builtin__s?
- From: garyjefferson123
- Accessing overridden __builtin__s?
- Prev by Date: Re: Integer keys in shelve
- Next by Date: Re: SSL/TLS - am I doing it right?
- Previous by thread: Re: Accessing overridden __builtin__s?
- Next by thread: Re: Accessing overridden __builtin__s?
- Index(es):
Relevant Pages
|