Some problems that you may have encountered in working with Python:
- __str__ and __repr__ methods:
There are two forms of the "toString" method in Python.
The first is __str__ which returns a string representation of an object.
This is the method that is called when you do "print obj".
The __repr__ method returns the "official" string represntation of the
object. The __repr__ method is called when the object is put in backquotes.
The idea is that the string produced by __repr__ should be a vaild Python
expression that could be used to reconstruct the object. You probably have
already noticed the differences between __str__ and __repr__ when you did
Part 2 of e03.
-
Printing numbers to k decimal places: Python uses the same format strings as
C, and then defines a % operator to translate variables given the format
strings. So, for example, the following prints a floating point number x to
4 decimal places: print "the value of x is %.4f" % x. The formatting
information in the string is %.4f where the f means that we expect to format
a floating point number, the .4 means display 4 decimal places.