pprint, __repr__ and inheritance, how does it works?

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Tue Feb 26 07:57:06 CET 2008


En Mon, 25 Feb 2008 10:19:43 -0200, Jorge Vargas <jorge.vargas at gmail.com>  
escribió:

> Hello list, I'm having a little bit of trouble making this all work  
> together.
>
> I have a base class in which I want to define the __repr__ for all
> subclasses which in this case will be to output all the __dict__
> atributes. Using [1] as reference I have come up with the following
> but it has 2 problems, first the type call it's not generating valid
> python code and second this doesn't seems to work with pprint as it's
> just printing the string as a one-liner making it equivalente as
> calling repr(obj)

Perhaps this is a bit closer to what you want, but still not perfect:

 from pprint import pprint,pformat

class node(object):
     def __init__(self,a,b,c):
         self.a,self.b,self.c = a,b,c
     def __repr__(self):
         return "%s(%s)" % (type(self).__name__, pformat(self.__dict__))

class extendedNode(node):
     def __init__(self,d,e):
         self.d,self.e = d,e

n = node(11111111111,2222222222,
   extendedNode(33333333333,4444444444444))
pprint(n)

-- 
Gabriel Genellina




More information about the Python-list mailing list