[Tutor] better way to make __repr__ methods for family of similar classes

Kent Johnson kent37 at tds.net
Tue Dec 13 23:34:21 CET 2005

Brian van den Broek wrote:
> Hi all,
> I think I must be doing something incorrectly. I have a family of 
> classes that all have the same arguments to their __init__ methods.
> I want to give them all __repr__ methods. From the docs, this method 
> should be written so that it looks like "a valid Python expression 
> that could be used to recreate an object with the same value (given an 
> appropriate environment)."
> What I have come up with is:
> class _A_Base(object):
>      def __init__(self, arg1, arg2):
>          # code here
>      def __repr__(self):
>          # str(self.__class__).split("'")[1] as
>          # str(self.__class__) returns something of the form
>          # <class 'module.classname'> and the split("'"[1] pulls
>          # out module.classname
>          return "%s%s" %(str(self.__class__).split("'")[1],
>   		                    (self.arg1, self.arg2))
> class A1(_A_Base):
>      def __init__(self, arg1, arg2):
>          # code here
> etc.
> This feels smelly and I think that the desire to give a family of 
> similar classes __repr__ methods must be common enough that there will 
> be a better way I am not thinking of.

You can use self.__class__.__module__ and self.__class__.__name__ 
instead of parsing str(self.__class__).

You could use a metaclass instead of a common base class. There are some 
dubious advantages to this approach:
- your classes don't have to inherit from a common base
- you can set the custom __repr__ for all classes in a module by 
assigning __metaclass__ at the module (instead of class) level.

For example:

class AutoRepr(type):
     def __init__(cls, name, bases, d):
         super(type, cls).__init__(name, bases, d)

         def __repr__(self):
             return '%s.%s(%s, %s)' % (cls.__module__, cls.__name__, 
self.arg1, self.arg2)

         setattr(cls, '__repr__', __repr__)

__metaclass__ = AutoRepr # This makes AutoRepr be the meta class of A1

class A1:
     def __init__(self, arg1, arg2):
         self.arg1 = arg1
         self.arg2 = arg2

a=A1(1, 2)

print repr(a)

__main__.A1(1, 2)


More information about the Tutor mailing list