J
Jorge Vargas
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)
from pprint import pprint
class node(object):
def __init__(self,a,b,c):
self.a,self.b,self.c = a,b,c
def __repr__(self):
# return str(type(self)) + "(" + repr(self.__dict__) + ')'
return "node(" + repr(self.__dict__) + ')'
class extendedNode(node):
def __init__(self,d,e):
self.d,self.e = d,e
def __repr__(self):
return "extendedNode(" + repr(self.__dict__) + ')'
print "big numbers are here to force the wrap"
nodes = node(11111111111,2,extendedNode(333333333333333,333333333333333))
print "not doing what I want"
pprint(nodes)
print "same as repr"
repr(nodes)
print "now with dicts"
nodes = {'a':11111111111,'b':2,'c':{'d':333333333333333,'e':333333333333333}}
print "works as it should"
pprint(nodes)
print "it's different indeed"
repr(nodes)
[1] http://www.oreillynet.com/onlamp/blog/2007/11/pymotw_pprint.html
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)
from pprint import pprint
class node(object):
def __init__(self,a,b,c):
self.a,self.b,self.c = a,b,c
def __repr__(self):
# return str(type(self)) + "(" + repr(self.__dict__) + ')'
return "node(" + repr(self.__dict__) + ')'
class extendedNode(node):
def __init__(self,d,e):
self.d,self.e = d,e
def __repr__(self):
return "extendedNode(" + repr(self.__dict__) + ')'
print "big numbers are here to force the wrap"
nodes = node(11111111111,2,extendedNode(333333333333333,333333333333333))
print "not doing what I want"
pprint(nodes)
print "same as repr"
repr(nodes)
print "now with dicts"
nodes = {'a':11111111111,'b':2,'c':{'d':333333333333333,'e':333333333333333}}
print "works as it should"
pprint(nodes)
print "it's different indeed"
repr(nodes)
[1] http://www.oreillynet.com/onlamp/blog/2007/11/pymotw_pprint.html