__str__ does not default to __repr__ if a base class has __str__
# In the code below, "a.__str__()" does not default to "a.__repr__() as # might be expected. It finds the "__str__" in the base class X. The # Reference Manual, section 3.3.1, says: # If a class defines __repr__() but not __str__(), then __repr__() is # also used when an 'informal' string representation of instances of # that class is required. # If the reader is in lawyer mode, the documentation is correct. # Is the current behavior what was intended? What should the behavior be? class X(object): def __str__(self): return 'str for X' def __repr__(self): return 'repr for X' class A(X): def __repr__(self): return 'repr for A' # __str__ = __repr__ x = X() a = A() print x.__str__() print x.__repr__() print a.__str__() print a.__repr__()
"Edward C. Jones"
# In the code below, "a.__str__()" does not default to "a.__repr__() as # might be expected. It finds the "__str__" in the base class X. The # Reference Manual, section 3.3.1, says: # If a class defines __repr__() but not __str__(), then __repr__() is # also used when an 'informal' string representation of instances of # that class is required. # If the reader is in lawyer mode, the documentation is correct. # Is the current behavior what was intended? What should the behavior be?
class X(object): def __str__(self): return 'str for X'
def __repr__(self): return 'repr for X'
class A(X): def __repr__(self): return 'repr for A'
# __str__ = __repr__
x = X() a = A() print x.__str__() print x.__repr__() print a.__str__() print a.__repr__()
_______________________________________________ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/python-python-dev%40m.gman...
"Edward C. Jones"
# In the code below, "a.__str__()" does not default to "a.__repr__() as # might be expected. It finds the "__str__" in the base class X. The # Reference Manual, section 3.3.1, says: # If a class defines __repr__() but not __str__(), then __repr__() is # also used when an 'informal' string representation of instances of # that class is required. # If the reader is in lawyer mode, the documentation is correct.
I don't think one has to be in lawyer mode to think that 'attribute x is
defined by class C' == 'C.x returns an object without exception', whether
the production is by direct lookup, inheritance, or calculation. It is
possible that 'defines' could be defined better.
The current behavior seems to be
try:
f = i.__str__
except AttributeError:
try:
f = i.__repr__
except AttributeError
f =
# Is the current behavior what was intended? What should the behavior be?
Since the current behavior is hard to see as a bug, I think it should stay as is, if it has been consistently so forever, even if something else might have been once preferred. Terry J. Reedy
# In the code below, "a.__str__()" does not default to "a.__repr__() as # might be expected. It finds the "__str__" in the base class X. The # Reference Manual, section 3.3.1, says: # If a class defines __repr__() but not __str__(), then __repr__() is # also used when an 'informal' string representation of instances of # that class is required. # If the reader is in lawyer mode, the documentation is correct. # Is the current behavior what was intended? What should the behavior be?
class X(object): def __str__(self): return 'str for X'
def __repr__(self): return 'repr for X'
class A(X): def __repr__(self): return 'repr for A'
# __str__ = __repr__
x = X() a = A() print x.__str__() print x.__repr__() print a.__str__() print a.__repr__()
This has never been different and the current outcome is exactly what was intended. The only case where __repr__ and __str__ act as each other's default is when there's no base class. And in any case, you're not supposed to call __str__ or __repr__ directly -- you should call str(x) or repr(x), etc. -- not that it makes a difference here. --Guido van Rossum (home page: http://www.python.org/~guido/)
Guido van Rossum wrote:
class X(object): def __str__(self): return 'str for X'
def __repr__(self): return 'repr for X'
class A(X): def __repr__(self): return 'repr for A'
# __str__ = __repr__
This has never been different and the current outcome is exactly what was intended. The only case where __repr__ and __str__ act as each other's default is when there's no base class.
The OP's example also shows how easy it is to create a class which returns the same thing for str(foo) and repr(foo). Simply write one of either __repr__ or __str__ and then assign the same function to the other one. Regards, Nick. -- Nick Coghlan | Brisbane, Australia Email: ncoghlan@email.com | Mobile: +61 409 573 268
participants (4)
-
Edward C. Jones
-
Guido van Rossum
-
Nick Coghlan
-
Terry Reedy