[Python-ideas] FormatRepr in reprlib for declaring simple repr functions easily

Ronny Pfannschmidt Ronny.Pfannschmidt at gmx.de
Wed May 30 17:03:31 CEST 2012


Hi,

i consider my utility class FormatRepr finished,
its currently availiable in
( http://pypi.python.org/pypi/reprtools/0.1 )

it supplies a descriptor that allows to simply declare __repr__ methods 
based on object attributes.

i think it greatly enhances readability for those things,
as its DRY and focuses on the parts *i* consider important
(e.E. what accessible attribute gets formatted how)

there is no need ot repeat attribute names or
care if something is a property,class-attribute or object attribute
(one of the reasons why a simple .format(**vars(self)) will not always work)

oversimplified example:


.. code-block:: python

    from reprtools import FormatRepr

    class User(object):
        __repr__ = FormatRepr("<User {name}>")

        def __init__(self, name):
            self.name = name



 >>> User('test')
<User test>



-- Ronny



More information about the Python-ideas mailing list