how to count lines in a file ?

Delaney, Timothy tdelaney at avaya.com
Thu Jul 25 18:51:03 EDT 2002


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

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.

Tim Delaney




More information about the Python-list mailing list