Extend the functionality of the print statement/function
Question: I want to print things to a certain specification, such as printing containers
(`[]`, `{}`, `()`) using their `str()` method instead of `repr()`. It's
trivial to write a function that will do that

    
    
    def str_print_list(alist):
        print "["+", ".join(map(str, alist))+"]"
    

but it'd be awfully nice if I could just extend or decorate either the
[`print`
statement](https://docs.python.org/2/reference/simple_stmts.html#print) or the
[`print()` function](https://docs.python.org/2/library/functions.html#print)
(in Python 2.7)

I could do something sort of like this, which works in python 3 (tested in
Python 3.2.3 at <http://www.compileonline.com/execute_python3_online.php>)

    
    
    class foo(): 
        def __str__(self):
            return "String"
        def __repr__(self):
            return "Repr"
    
    print([foo()])
    
    def my_decorator(func):
        def inner(alist):
            if isinstance(alist, list):
                return func("["+", ".join(map(str, alist))+"]")
            else:
                return func(alist)
        return inner
    
    print = my_decorator(print)
    print([foo()])
    

Which gives me an output of

    
    
    [Repr]
    [String]
    

but gives me a syntax error in python 2.7.6 at

    
    
    print = my_decorator(print)
    

until I import

    
    
    from __future__ import print_function
    

which makes sense, because statements can't (as far as I know) be decorated or
reassigned.

So my question is

  1. Is it possible to decorate the print statement so I don't have to import the function?
  2. Is this even a good idea, or should I just be more explicit and use my `str_print_list()` function? In this case I know _for a fact_ that this would be for my personal use and would be used primarily for debugging 


Answer:   1. No, statements can't be changed.
  2. No. The way to handle this is to make your own `list` and pass that around -- this is exactly what inheritance is made for.

Like this:

    
    
    class MyPrintableList(list):
        def __repr__(self):
            return "[{}]".format(",".join(self))
    
    print MyPrintableList([foo()])
    

