标签云

微信群

扫码加入我们

WeChat QR Code

我们可以重载的类对象[复制]行为

This question already has an answer here:

I know we can overload behavior of instances of a class, e.g. -

class Sample(object):  pass
s = Sample()
print s
<__main__.Sample object at 0x026277D0>
print Sample
<class '__main__.Sample'>

We can change the result of print s:

class Sample(object):
  def __str__(self):
    return "Instance of Sample"
s = Sample()
print s
Instance of Sample

Can we change the result of print Sample?