0

In python when you print, you're really just calling sys.stdout's write() method. Its easy to make your own custom class that gets written to

import sys

class MyClass(object):

    def write(self, astring):
        # do stuff

sys.stdout = MyClass()

What, if any, specifications are there for the write() method? This question seems to indicate that it just needs to take a string input. Are there any other rules or specifications as to what the method should accept and do?

Community
  • 1
  • 1
Dan Oberlam
  • 2,435
  • 9
  • 36
  • 54
  • 5
    if it's called like write and takes the same parameters as write then it's a duck. – soulcheck Sep 20 '14 at 21:32
  • I think that it is it - take a string and don't return anything - it should of course have side effects of impacting the return values of methods such as seek etc - and it should raise exceptions if required too. – Tony Suffolk 66 Sep 20 '14 at 21:32
  • i don't think any method is required to raise exceptions. The only requirement can be that if the exception is raised then it's of some predefined type. – soulcheck Sep 20 '14 at 21:35

0 Answers0