我有一个代码,其中几乎没有类。在 下SecondClass
,有一个函数叫做plot_image
。在这个函数下,定义了三个变量my_x, my_y, my_z。我试图使这些变量在同一个类中调用的其他一些函数中可访问。这是一个伪代码段形式的非工作示例:get_histogram
import some_modules_in_here #these are required for classes below
#===================================================================================================
# utility
#===================================================================================================
class FirstClass(something.something.SayClass):
def file1(self, some_arguments):
pros = do_something
return pros
#===================================================================================================
# visualize
#===================================================================================================
class SecondClass(something.something.SayClass):
def plot_image(self, some_parameter):
if some_parameter is not None:
my_x = some_value1
my_y = some_value2
my_z = some_value3
def get_histogram(self, some_parameter, some_other_parameter, return_info):
if '3d' in some_parameter:
do_this
else:
do_that
if some_other_parameter:
calculate_something
if return_info:
for k, l, m in zip(my_x, my_y, my_z):
do_some_stuff
print(something_in_here)
return_value = do_something_else_in_here
return return_value
.
.
.
def print_values(self):
'''
write some values of image to file.
'''
Second = SecondClass()
global
我尝试了函数顶部的关键字,plot_image
但这并没有为我提供错误信息:
“NameError:名称‘my_x’未定义。”
解决办法是什么?