What a weird thing !!?

Ken Seehof kens at sightreader.com
Tue Feb 22 16:59:03 EST 2000


What exactly is it that you want to do?  It is unlikely that you really want
to overload print (which is impossible anyway).  However there is almost
certainly a way to do what you want.  If the following does not anwer your
question, please explain exactly what you want to do (i.e. the end result)
and provide an example.

1. Change what gets printed for an instance of a particular class?
2. Change where things get printed to (e.g. message window, socket etc...)?
3. Change the format of what you are trying to print (e.g. no spaces between
items, etc.)

The answer to 1. is: overload __repr__() or __str__() for your class.

The answer to 2. is: replace sys.stdout.write (you still use print, but this
will change where the print output goes).  This won't change what gets
printed, it only changes where everything gets printed to when you use the
print command.  You don't -use- sys.stdout.write instead of print;
you -replace- sys.stdout.write, and -then- use print.  Changing
sys.stdout.write changes the effect of using print.  Only super-geeks are
allowed to do this.

The answer to 3. is: learn how to use the % format operator.  Play around in
the interpreter and you will learn everything quickly.
>>> a = 'A'
>>> b = 'B'
>>> pi = 3.1415926535897932
>>> print "%s%s" % (a,b)
AB
>>> print "pi = %1.4f" % pi
3.1416
>>> print "pi = %1.2" % pi
3.14

Another answer to 3. is: learn to use the + operator for strings
>>> print a+b
AB


Gerrit Holl <gerrit.holl at pobox.com> wrote in message
news:20000218225338.A9457 at stopcontact.palga.uucp...
> Florent Ramière wrote on 950907619:
> > Thanks for your answers,
> >
> >     in fact as Moshe Zadka told me, it is a documented feature ...
(sorry)
> >     Well, i should have read more carefully the doc (re-sorry)
> >     And is there a solution to my problem ?
> >     And sys.stdout.write is not a good solution for me (i need to keep
> > printing with print)
> >
> >     Is there an easy way to override the print statement (i saw it one
time
> > in the news, but i can not find it)
>
> No. You can't override statements. print is a statement as if, while and
> for are statements.
>
> regards,
> Gerrit.
>
> --
> cat: /home/gerrit/.signature: No such quote or joke
>





More information about the Python-list mailing list