A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://mail.python.org/pipermail/python-dev/2004-May/044861.html below:

[Python-Dev] __str__ does not default to __repr__ if a base class has __str__

[Python-Dev] __str__ does not default to __repr__ if a base class has __str__ [Python-Dev] __str__ does not default to __repr__ if a base class has __str__Edward C. Jones edcjones at erols.com
Mon May 17 11:00:11 EDT 2004
# 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__()




More information about the Python-Dev mailing list

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4