我在如何在模块之间共享类实例方面遇到了问题。下面不是实际的代码,它是我试图做的简化表示。有问题的变量是这个地块。如果将其设为全局,则会出现“global name not defined”类型错误。如果我不使用全局,那么shapes模块中的“draw”方法就找不到它。你知道吗
调用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)
还有这个:
应该变成:
条件是
main.main()
以前被调用过。你知道吗我对matplotlib不太熟悉,但看起来有一个类似的问题Add cylinder to plot。这个代码怎么样?你知道吗
相关问题 更多 >
编程相关推荐