M
MisterPete
I created an object that inherits from file and was a bit surprised to
find that print seems to bypass the write method for objects
inheriting from file. An optimization I suppose. Does this surprise
anyone else at all or am I missing something?
import sys
class FromObject(object):
def write(self, string):
# this works fine, gets called by print
sys.stdout.write("FromObject: " + string)
class FromFile(file):
def __init__(self, name, mode='w'):
file.__init__(self, name, mode)
def write(self, string):
# this does not get called by print
sys.stdout.write("FromFile: " + string)
a = FromObject()
b = FromFile("test.txt")
a.write("Foo\n") # works as expected
b.write("Bar\n") # works as expected
print >> a, "Baz\n"
# "FromFile: Baz\nFromFile:\n" written to stdout. That's fine.
print >> b, "Qux\n"
b.flush()
# "Qux\n" written to test.txt. b.write wasn't called
find that print seems to bypass the write method for objects
inheriting from file. An optimization I suppose. Does this surprise
anyone else at all or am I missing something?
import sys
class FromObject(object):
def write(self, string):
# this works fine, gets called by print
sys.stdout.write("FromObject: " + string)
class FromFile(file):
def __init__(self, name, mode='w'):
file.__init__(self, name, mode)
def write(self, string):
# this does not get called by print
sys.stdout.write("FromFile: " + string)
a = FromObject()
b = FromFile("test.txt")
a.write("Foo\n") # works as expected
b.write("Bar\n") # works as expected
print >> a, "Baz\n"
# "FromFile: Baz\nFromFile:\n" written to stdout. That's fine.
print >> b, "Qux\n"
b.flush()
# "Qux\n" written to test.txt. b.write wasn't called