¿Por qué str(A())
aparentemente llama A.__repr__()
y no dict.__str__()
en el ejemplo siguiente?Llamada de método de clase base de Python: comportamiento inesperado
class A(dict):
def __repr__(self):
return 'repr(A)'
def __str__(self):
return dict.__str__(self)
class B(dict):
def __str__(self):
return dict.__str__(self)
print 'call: repr(A) expect: repr(A) get:', repr(A()) # works
print 'call: str(A) expect: {} get:', str(A()) # does not work
print 'call: str(B) expect: {} get:', str(B()) # works
Salida:
call: repr(A) expect: repr(A) get: repr(A)
call: str(A) expect: {} get: repr(A)
call: str(B) expect: {} get: {}
http://www.python.org/dev/peps/pep-3140/ – bernie