By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,551 Members | 1,159 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,551 IT Pros & Developers. It's quick & easy.

inherit from file and create stdout instance?

P: 5
How can I inherit from file but stil create an instance that writes to stdout?
I'm writing a file-like object that has verbosity options (among some other things). I know I could just set self.file to a file object rather than inheriting from file. I started with something like this (simplified for clarity):
Expand|Select|Wrap|Line Numbers
  1. class Output(object):
  2.     def __init__(self, file=sys.stdout, verbosity=1):
  3.         self.verbosity = verbosity
  4.         self.file = file
  6.     def write(self, string, messageVerbosity=1):
  7.         if messageVerbosity <= self.verbosity:
  8.             self.file.write(string)
  9. ...
  11. but it is frustrating me that if I try to inherit from file it works fine for regular files but I can't figure out a clean way to instantiate an object that writes to stdout using sys.stdout. something like the following:
  13. class Output(file):
  14.     def __init__(self, name, mode='w', buffering=None, verbosity=1):
  15.         super(Output, self).__init__(name, mode, buffering)
  16.         self.verbosity = 1
  18.     def write(self, string, messageVerbosity=1):
  19.         if messageVerbosity <= self.verbosity
  20.             super(Output, self).write(string)

I hope that's clear. Is it just a bad idea to inherit from file to create a class to write to stdout or am I missing something? Any insight is appreciated.

May 11 '07 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.