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

Grant Edwards grante at visi.com
Wed Apr 30 10:54:24 EDT 2003


In article <b8onf8$b7d$1 at panix2.panix.com>, 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 = self.open(...)", 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?

----------------------testit.py----------------------
class C:
    def __init__(self):
        print "C.init()"
        print open
        print self.open
    def open(self,fname):
        print "C.open(%s)" % fname
        print open
        print self.open
        return open(fname)

c = C()
f = c.open("foo")
print f
---------------------------------------------

$ python2 testit.py
C.init()
<type 'file'>
<bound method C.open of <__main__.C instance at 0x81667c4>>
C.open(foo)
<type 'file'>
<bound method C.open 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?
                               visi.com            




More information about the Python-list mailing list