[Tutor] Clueless

Rich Krauter rmkrauter at yahoo.com
Sat Mar 13 23:02:54 EST 2004


On Sat, 2004-03-13 at 20:44, Marilyn Davis wrote:
> I can't even give this one a good subject line, I'm so clueless.
> 
> I'm trying to make something meaningful using __getattribute__.
> What a bear to stop all the infinite loops!
> 
> But, finally, I have all that under control. 
> 
> So, my classes are:
> 
>   Logger                       object
>    /|\                        /|\  /|\
>     |                          |    |
>   Watched  --------------------     |
>    /|\                            list
>     |                              /|\
>     |                               |
>   WatchedList  ---------------------
> 
> All is cool as long as I don't implement Logger.__del__
> 
> But if I do, crash-city when an object goes away:
> 
> Exception exceptions.AttributeError: "'NoneType' object has no
> attribute 'close'" in <bound method WatchedList.__del__ of []> ignored
> 
> If anyone has time and inclination, could you help?
> 
> Thank you so much.
> 
> Marilyn Davis
> 
> #!/usr/bin/env python2.2
> '''New style classes have __getattribute__ which intercept
> all references to attributes, ones that exist and don't
> exist.  We'll make a Watched class that logs all accesses and
> assignments.'''
> 
> import time
> 
> class Logger:
>     def __init__(self, name):
>         self.file = open(name, 'w')
>     def logit(self, entry):
>         self.file.write(time.ctime(time.time()))
>         self.file.write('\n' + entry + '\n')
>         self.file.flush()
> #    def __del__(self):
> #        self.file.close()
> 

Hi Marilyn,

I added this to the Watched class, and it seemed to help:

def __del__(self):
        Logger.__del__(self.log)

Also, I wouldn't use 'file' as an attribute name. It's in synonym for
'open'.

Hope that helps.

Rich



More information about the Tutor mailing list