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

print bypasses calling write method for objects inheriting from file?

P: n/a
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 :(

May 30 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
MisterPete wrote:
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?
No, your analysis is correct, though I'd consider optimization an euphemism
for bug here. Noone was annoyed enough to write a patch, it seems.

Peter

May 30 '07 #2

P: n/a
En Wed, 30 May 2007 04:24:30 -0300, Peter Otten <__*******@web.de>
escribió:
>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?

No, your analysis is correct, though I'd consider optimization an
euphemism
for bug here. Noone was annoyed enough to write a patch, it seems.
A one-line patch, I guess, PyFile_CheckExact instead of PyFile_Check. Or a
few lines, checking if the write method is still the builtin one. As this
is the third time I see this question I'll try to submit the patch.

--
Gabriel Genellina

May 30 '07 #3

P: n/a
Gabriel Genellina wrote:
En Wed, 30 May 2007 04:24:30 -0300, Peter Otten <__*******@web.de>
escribió:
>>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?

No, your analysis is correct, though I'd consider optimization an
euphemism
for bug here. Noone was annoyed enough to write a patch, it seems.

A one-line patch, I guess, PyFile_CheckExact instead of PyFile_Check. Or a
few lines, checking if the write method is still the builtin one. As this
is the third time I see this question I'll try to submit the patch.
Good idea ;) Don't know if it is still applicable, but here's a patch by
Jeff Epler:

http://groups.google.com/group/comp....d46ff2e05e1476

Peter
May 30 '07 #4

P: n/a
On May 31, 5:54 am, Peter Otten <__pete...@web.dewrote:
Gabriel Genellina wrote:
En Wed, 30 May 2007 04:24:30 -0300, Peter Otten <__pete...@web.de>
escribió:
>I created an object that inherits from file and was a bit surprised to
find thatprintseems 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?
No, your analysis is correct, though I'd consider optimization an
euphemism
for bug here. Noone was annoyed enough to write a patch, it seems.
A one-line patch, I guess, PyFile_CheckExact instead of PyFile_Check. Or a
few lines, checking if the write method is still the builtin one. As this
is the third time I see this question I'll try to submit the patch.

Good idea ;) Don't know if it is still applicable, but here's a patch by
Jeff Epler:

http://groups.google.com/group/comp....d46ff2e05e1476

Peter
Thanks for the quick replies! I forgot to mention that I was using
Python v2.4.3 but it sounds like it is still a problem anyway. Thanks
Peter and Gabriel.

May 30 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.