Elegantly subsplitting a sequence

Skip Montanaro skip at pobox.com
Fri May 30 11:37:38 EDT 2003


    Steve> And similarly, to provide a nice repr() of an integer, what about {
    ...

That should be "a nice str() of an integer".  Where possible, repr() has the
property that the string generated can be eval'd back to an object of the
desired type.  This isn't obvious with integers where the output of repr()
and str() is the same, but is with strings:

    >>> eval(repr("abc"))
    'abc'
    >>> eval(str("abc"))
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
      File "<string>", line 0, in ?
    NameError: name 'abc' is not defined

Also, providing default args to __repr__ isn't terribly helpful in the
common case since repr() only takes a single argument:

    >>> repr(i,3,",")
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
    TypeError: repr() takes exactly one argument (3 given)

Probably better to just rename __repr__ to "format".

Skip





More information about the Python-list mailing list