Python 如何与另一个模块中的类共享在main中创建的实例

Python 如何与另一个模块中的类共享在main中创建的实例,python,class,share,instance,global,Python,Class,Share,Instance,Global,我在如何在模块之间共享类实例方面遇到了问题。下面不是实际的代码,它是我试图做的一个简化表示。所讨论的变量是Plot。如果将其设为全局,则会出现“未定义全局名称”类型错误。如果我不使用全局,那么“形状”模块中的“绘制”方法将找不到它 调用draw()方法并尝试执行'thePlot'方法'plot'时,会出现错误 * Main module ***************** import matplotlib as plt import plotter import shapes main():

我在如何在模块之间共享类实例方面遇到了问题。下面不是实际的代码,它是我试图做的一个简化表示。所讨论的变量是Plot。如果将其设为全局,则会出现“未定义全局名称”类型错误。如果我不使用全局,那么“形状”模块中的“绘制”方法将找不到它

调用draw()方法并尝试执行'thePlot'方法'plot'时,会出现错误

* Main module *****************
import matplotlib as plt
import plotter
import shapes

main():
    thePlot = plotter.plotter()
    cyl = shapes.cylinder(r, c, n, color)
    cyl.draw()
    plt.show()

* shapes module *******************
import main
import plotter

class shapes(self):
    def __init__(self):
        pass
    def cylinder(r, c, n, color):
    self.r = r
    self.c = c
    self.n = n
    self.color = color

def draw(self):
    self.x = calculate list of x coordinates
    self.y = calculate list of y coordinates
    self.z = calculate list of z coordinates
    global thePlot
    * This line causes the error
    thePlot.plot(self.x, self.y, self.z, self.color)

* plotter module ******************
import matplotlib as plt

class plotter(self):
    def __init__(self):
        self.fig = plt.figure()
        self.ax = fig.add_subplot(111, projection='3d')

    def plot(self, x, y, z, color):
        self.ax.plot_wireframe(x, y, z, color)
这是:

global thePlot
* This line causes the error
thePlot.plot(self.x, self.y, self.z, self.color)
应成为:

main.thePlot.plot(self.x, self.y, self.z, self.color)

条件是以前调用过
main.main()

我对matplotlib不熟悉,但看起来有一个类似的问题。这个代码怎么样

import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D


fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

"""
def cyl():
    x = None
    y = None
    z = None
    return
"""
def cyl():
    x=np.linspace(-1, 1, 100)
    z=np.linspace(-2, 2, 100)
    Xc, Zc=np.meshgrid(x, z)
    Yc = np.sqrt(1-Xc**2)
    return Xc, Yc, Zc

Xc, Yc, Zc = cyl()

#draw parameters                                                                                                                                                          
rstride = 20
cstride = 10
ax.plot_surface(Xc, Yc, Zc, alpha=0.2, rstride=rstride, cstride=cstride)
ax.plot_surface(Xc, -Yc, Zc, alpha=0.2, rstride=rstride, cstride=cstride)

ax.set_xlabel("X")
ax.set_ylabel("Y")
ax.set_zlabel("Z")
plt.show()

然后我得到一个“未定义的变量来自导入:thePlot”错误。@TomSpargo这看起来像是PyDev错误。这并不意味着脚本将无法工作。要使
main.plotter
在使用时工作,它必须存在,即您必须已经调用
main.main
函数,或者执行
thePlot=plotter.plotter()
不在
main.main()
内,而是在模块级别。是的,如果我将ThePlotter=plotter.plotter放在if name=='main之前。。。声明,然后它的工作。我认为这就是所谓的单态对象。这就是我要做的。谢谢谢谢,但这更多的是关于如何在模块之间共享实例,而不是Matplotlib问题。
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D


fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

"""
def cyl():
    x = None
    y = None
    z = None
    return
"""
def cyl():
    x=np.linspace(-1, 1, 100)
    z=np.linspace(-2, 2, 100)
    Xc, Zc=np.meshgrid(x, z)
    Yc = np.sqrt(1-Xc**2)
    return Xc, Yc, Zc

Xc, Yc, Zc = cyl()

#draw parameters                                                                                                                                                          
rstride = 20
cstride = 10
ax.plot_surface(Xc, Yc, Zc, alpha=0.2, rstride=rstride, cstride=cstride)
ax.plot_surface(Xc, -Yc, Zc, alpha=0.2, rstride=rstride, cstride=cstride)

ax.set_xlabel("X")
ax.set_ylabel("Y")
ax.set_zlabel("Z")
plt.show()