我没有显示整个类的问题是很难做出简单的,但下面是关键,我相信:我创建新引用同一个对象,而不是新的对象
class helper:
sharex = False
sharey = False
_objects = []
_figures = []
_axes = []
def __init__(self):
pass
def create(self):
figure, axes = plt.subplots(nrows=self.nrows,
ncols=self.ncols,
sharex=self.sharex,
sharey=self.sharey)
self._figures.append(figure)
self._axes.append(axes)
的以下说明应该我的问题:
test = plotting.helper()
test._axes
Out[9]: []
test.create()
test._axes
Out[10]:
[array([<matplotlib.axes._subplots.AxesSubplot object at 0x103dd1940>,
<matplotlib.axes._subplots.AxesSubplot object at 0x10c4a5278>,
<matplotlib.axes._subplots.AxesSubplot object at 0x10c1889b0>,
<matplotlib.axes._subplots.AxesSubplot object at 0x11c52eeb8>,
<matplotlib.axes._subplots.AxesSubplot object at 0x11c56f4a8>,
<matplotlib.axes._subplots.AxesSubplot object at 0x11c5b7be0>], dtype=object)]
test2 = plotting.helper()
test2._axes
Out[11]:
[array([<matplotlib.axes._subplots.AxesSubplot object at 0x103dd1940>,
<matplotlib.axes._subplots.AxesSubplot object at 0x10c4a5278>,
<matplotlib.axes._subplots.AxesSubplot object at 0x10c1889b0>,
<matplotlib.axes._subplots.AxesSubplot object at 0x11c52eeb8>,
<matplotlib.axes._subplots.AxesSubplot object at 0x11c56f4a8>,
<matplotlib.axes._subplots.AxesSubplot object at 0x11c5b7be0>], dtype=object)]
第二个目的应创建空,并且没有存储在_axes
任何轴。然而,它不仅包含坐标轴,而且与(假定)独立的其他对象一样,也是相同的。
为什么我不创建独立的对象?
我会建议你阅读一篇Python OOP教程,它将解释大部分内容。 – jonrsharpe