16 November 2010

Python toString() class method?

Again, this is a common question regarding to Python OOP. In Java, we often overwrite a class method toString() for our own class, is there an equivalent thing in Python?

The answer is ... YES!

Just by redefining an special "__str__(self):" method in your own class, you can have your own human-readable string representation of your class object! What a beauty!

No comments: