str(list)

Trevor Blackwell tlb at anybots.com
Wed Jun 9 16:42:51 EDT 2004


I wish that str of a list would call str on the elements, rather than
repr. Currently, list has only a repr function so it ends up calling
repr on its members.

The way to fix this would be to add a list_str in Objects/listobject.c
similar to list_repr.

An example:

class mytype:
    def __init__(self, x):
        self.x=x

    def __str__(self):
        return "mytype(%s)" % self.x

foo=mytype("foo")
bar=mytype("bar")

print foo # Prints mytype(foo)
print bar # Prints mytype(bar)
print [foo,bar]	# Prints [<__main__.mytype instance at 0x81b21ac>,
		# <__main__.mytype instance at 0x81b216c>]


-- 
Trevor Blackwell <tlb at anybots.com>





More information about the Python-list mailing list