2016-10-22 162 views
0

我没有显示整个类的问题是很难做出简单的,但下面是关键,我相信:我创建新引用同一个对象,而不是新的对象

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任何轴。然而,它不仅包含坐标轴,而且与(假定)独立的其他对象一样,也是相同的。

为什么我不创建独立的对象?

+0

我会建议你阅读一篇Python OOP教程,它将解释大部分内容。 – jonrsharpe

回答

0

当您在类声明中声明_axes时,将其设为静态变量,意味着它与所有实例都是相互关联的。 您想要将_axes的声明移至__init__函数。

+0

这个问题已经被标记为一个骗局。 –

+0

是的,我现在看到它...没有通知我在移动应用程序。 – Sawel

相关问题