Python: file with programmer-defined attributes -


After

Using Python 2.5, I want to create a temporary file, but add (& amp; modify) my own responsibility telling. I've tried the following:

  class TempFileWithAttributes (): __slots__ = [ '_tempFile', 'value'] def __init__ (self): self._tempFile = tempfile.TemporaryFile () object. __setattr__ (self, '_tempFile', tempfile.TemporaryFile ()) object .__ setattr__ (self, 'value', 123) def __getattr__ (self, name): If name == 'value': object .__ getattr__ (self, 'value') Elif name == '_tempFile': return getattr (self._tempFile, name) def __setattr__ (self, name, NewValue): if name == 'value': object .__ setattr__ (itself the 'value' NewValue) Elif name == '_tempFile': Setattr (Selfk_tempFile, NewValue) MyFile = TempFileWithAttributes () MyFilekwrite ( "! Hello, Jason ') print myFile.Value myFile.Value = 456 print myFile.Value myFile.seek (0, OsSEK_END) print myFile.tell ()  

However, welcome to follow me with error number Esh ing:

  object .__ setattr__ (can not apply themselves, '_tempFile', tempfile.TemporaryFile ()) typos: example __setattr__ object  

I also tried subclassing file , but that was not working.

Any suggestions?

why you __setet____ at all Overriding?! You are not doing anything useful in your override - and your override of __ getattr __ is not very useful either. I think what you want is something like this:

  & gt; & Gt; & Gt; Class TempFileWithAttributes (object): ... __slots__ = [ '_tempFile', 'value'] ... def __init __ (self): ... self._tempFile = tempfile.TemporaryFile () ... self.Value = 123 . ..df __getattr __ (self, name): ... return Getattr (self._tempFile, name) ...  

This rest of your sample code, probably as Intent in


Comments

Popular posts from this blog

php - multilevel menu with multilevel array -

c# - TypeConverter in propertygrid only converts from string, not to -

jQuery UI: Datepicker month format -