How to call built in function 'open' from inside object that has 'open' method?

Grant Edwards grante at
Wed Apr 30 16:54:24 CEST 2003

In article <b8onf8$b7d$1 at>, Aahz wrote:

>>> More details on reasons. I have met this problem trying to write a
>>> simple script use ActiveScripting inside IE.  I am trying "file =
>>> open(...) but because context window object has 'open' method that
>>> just opens window it is called first.
>>Unless you're typing "file =", the class you're in has
>>nothing to do with it.
> Not quite true; what happens with
>     class C:
>         def open(self, name):
>             f = open(name)

When I do it, it calls the builtin open().  What happens when
you do it?
class C:
    def __init__(self):
        print "C.init()"
        print open
    def open(self,fname):
        print "" % fname
        print open
        return open(fname)

c = C()
f ="foo")
print f

$ python2
<type 'file'>
<bound method of <__main__.C instance at 0x81667c4>>
<type 'file'>
<bound method of <__main__.C instance at 0x81667c4>>
<open file 'foo', mode 'r' at 0x8166178>

Grant Edwards                   grante             Yow!  What GOOD is a
                                  at               CARDBOARD suitcase ANYWAY?

More information about the Python-list mailing list