擅长:python、mysql、java
<p>在控制台中,您将获得对象的表示,它由<code>__repr__</code>负责。<code>__str__</code>用于打印:</p>
<pre><code>>>> class A:
def __str__(self):
return 'spam'
>>> A()
<__main__.A object at 0x0107E3D0>
>>> print(A())
spam
>>> class B:
def __repr__(self):
return 'ham'
>>> B()
ham
>>> print(B())
ham
>>> class C:
def __str__(self):
return 'spam'
def __repr__(self):
return 'ham'
>>> C()
ham
>>> print(C())
spam
</code></pre>