473,396 Members | 1,864 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

print bypasses calling write method for objects inheriting from file?

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
4 1326
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Dan Williams | last post by:
Hi people I'm getting a little annoyed with the way the print function always adds a space character between print statements unless there has been a new line. The manual mentions that "In some...
30
by: Martin Bless | last post by:
Why can't we have an additional 'print' statement, that behaves exactly like 'print' but doesn't insert that damn blank between two arguments? Could be called 'printn' or 'prin1' or 'prinn'...
5
by: vegetax | last post by:
How can i use cgi'like print statement in a multitreaded web framework? each thread has its own Servlet instance with request/response objects, sys.stdout = self.response(which is a file like...
4
by: Gibby Koldenhof | last post by:
Hiya, I'm setting up some code in the spirit of Design Patterns, OOP, etc. All nice and well, it handles pretty much all OO style things and serves my purposes well. There's one last final...
5
by: Dave Veeneman | last post by:
I'm using inheritance more than I used to, and I find myself calling a lot of base class methods. I generally call a base method from a dreived class like this: this.MyMethod(); I'm finding...
12
by: torbs | last post by:
Hi I have a a function with several methods. For simplicity it looks a bit like this: super.prototype.aProperty="HELLO"; super.prototype.returnValue = function () { return 2;
3
by: AWasilenko | last post by:
I'm still in the process of learning python via a handful of books I bought. One book I am reading just introduced Base Class Methods. I found that I needed more understanding on this concept and...
7
by: bukzor | last post by:
I was trying to change the behaviour of print (tee all output to a temp file) by inheriting from file and overwriting sys.stdout, but it looks like print uses C-level stuff to do its writes which...
16
by: raylopez99 | last post by:
I am running out of printing paper trying to debug this...it has to be trivial, but I cannot figure it out--can you? Why am I not printing text, but just the initial string "howdy"? On the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.