how to count lines in a file ?

Michael Gilfix mgilfix at eecs.tufts.edu
Thu Jul 25 22:48:16 EDT 2002


On Fri, Jul 26 @ 08:51, Delaney, Timothy wrote:
> > From: Michael Gilfix [mailto:mgilfix at eecs.tufts.edu]
> > 
> >   So, out of curiosity, if I define a metaclass on top of some
> > file object, is this still considered broken code? Is the
> > __del__ semantics meaningless? Would this object ever become
> > part of garbage collection?
> > 
> >   class file:
> >     def __init__ (self, path):
> >       self.file = open (path)
> >     def __getattr__ (self, name):
> >       return getattr (self.file, name)
> >     def __setattr__ (self, name, value):
> >       return setattr (self, name, value)
> >     def __del__ (self):
> >       self.file.close ()
> 
> First of all, you shouldn't use the name "file" for your class (file is a
> builtin name, and is an alias for open). Secondly, you're using the term

  Ah, wasn't aware of that alias. At any rate, it's just an example.

> "metaclass" incorrectly here (at least in respect to python) - the term you
> are looking for is "proxy", "delegate" or even "adaptor".

  Ok. Proxy class sound sgood to me.

> There are no problems with the above. You have created a normal delegation
> class. If an instance does not participate in a cycle (in CPython) it will
> be collected as soon as the last reference to it goes away. If it does
> participate in a cycle, it will probably be collected at some later stage.
> In Jython, all objects participate in garbage collection.

  Sorry, I'm a little unclear on this one. Do I actually gain anything
by creating this proxy class over using the regular file object? Does
the use of __del__ add anything? Meaning, will the file.close call
definitely be called at the "some later stage" or whenever the object
is freed?

                    -- Mike

-- 
Michael Gilfix
mgilfix at eecs.tufts.edu

For my gpg public key:
http://www.eecs.tufts.edu/~mgilfix/contact.html




More information about the Python-list mailing list