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

Alan Kennedy alanmk at hotmail.com
Wed Apr 30 10:06:17 EDT 2003


vadim wrote:
> 
> I am new to Python and do not know the answer to very
> simple how to 'open' from inside another object that
> has 'open' method. I know that in C++ I should use
> following syntax ::open.
> 
> 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. Sorry for simple question. I was trying to find
> the answer but either it is not on the surface or too simple.

There should be no clash between the names if your object has
an "open" method.

When an object has an "open" method, it is either referred to
by

self.open()

or 

objname.open()

So you should be able to call (the builtin) open from within 
your object, and there should be no name clash, because you have not
explicitly specified the open method on your object (i.e. "self").

Here is an example:

class  Sketch:

  def  __init__(self, filename):
    # Note this calls __builtin__.open
    self.outchan = open(filename, "wt") 

  def  open(self):
    self.outchan.write("Opening window\n")

  def  doit(self, o):
    self.outchan.write("Goodbye, %s!\n" % o)

  def  close(self):
    self.outchan.write("Closing window\n")
    self.outchan.close()

  def  defenestrate(self, o):
    #  Note this calls self.open
    self.open()              
    self.doit(o)
    self.close()

>>> s = Sketch('sketch.log')
>>> s.defenestrate('tabby')

HTH,

-- 
alan kennedy
-----------------------------------------------------
check http headers here: http://xhaus.com/headers
email alan:              http://xhaus.com/mailto/alan




More information about the Python-list mailing list