exec - return value of expression

Josiah Carlson jcarlson at nospam.uci.edu
Sat Feb 14 02:10:16 CET 2004


> One solution would be to change the __repr__-Methods of the objects
> which are to be prettyprinted.  This should work, but I would like to
> prettyprint some builtin objects too, like a list of formulas for
> example.  And I don't know how to change the __repr__ of builtin
> lists.

Here's a kludge...if you are willing to manually instantiate all lists 
first.

 >>> _list = list
 >>> class list(_list):
...     def __repr__(self):
...         orig = _list.__repr__(self)
...         return "blah "+orig
...
 >>> a = list()
 >>> a
blah []
 >>> a.append('hello')
 >>> a
blah ['hello']
 >>> a.extend(['world', 'blah'])
 >>> a
blah ['hello', 'world', 'blah']

> Any hints on how to solve this Problem in an elegant way are greatly
> appreciated.

It is not elegant, but it may work for you.

  - Josiah



More information about the Python-list mailing list