Files
python3-cookbook/cookbook/c08/p01_str_represent.py

24 lines
356 B
Python
Raw Normal View History

2015-01-27 11:13:14 +08:00
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
"""
Topic: 实例的字符串显示
Desc :
"""
class Pair:
def __init__(self, x, y):
self.x = x
self.y = y
def __repr__(self):
return 'Pair({0.x!r}, {0.y!r})'.format(self)
def __str__(self):
return '({0.x!s}, {0.y!s})'.format(self)
p = Pair(3, 4)
print(p)