擅长:python、mysql、java
<p>这里有一个小窍门给你。如果从<code>tuple</code>继承,那么返回类型仍然可以是<code>tuple</code>,那么只需要<code>Point</code>的包装器和访问<code>tuple</code>属性的方法。这可以通过<code>namedtuple</code>或者像这样做:</p>
<pre class="lang-py prettyprint-override"><code>In [52]: class Coord(tuple):
...: def __init__(self, xy):
...: super().__init__()
...: x, y = xy
...: self._x = x
...: self._y = y
...: @property
...: def x(self):
...: return self._x
...: @property
...: def y(self):
...: return self._y
...:
In [53]: class Point:
...: def __init__(self, coord: Coord):
...: self.coord = coord
...:
In [54]: p = Point(Coord((5,12)))
In [55]: p.coord
Out[55]: (5, 12)
In [56]: p.coord.x
Out[56]: 5
In [57]: p.coord.y
Out[57]: 12
</code></pre>