toString()

David Allen s2mdalle at titan.vcu.edu
Wed Jun 7 21:26:42 EDT 2000


Sorry if this is a FAQ - I can't seem to find the answer anywhere.

Is there such thing as a toString() for objects like java has that
will be automatically called when an object is used "as a string"?

I.e. when you create an object, __init__ gets called automatically,
so if I say:

x = Spam()
print x

can I put a method in Spam such that it will print out something
useful based off of the class fields rather than the ugly data 
definition?

Thanks,
-- 
David Allen
http://opop.nols.com/




More information about the Python-list mailing list