关闭不传递值。 python 3
Closure not passing values. python 3
我正在尝试编写一个 closure/nested 函数来绘制许多类似的图。现在似乎并非所有值都被传递,因为我尝试传递的变量之一得到 'UnboundLocalError':
我的代码如下:
def PlotFunc(x_name, y_name, fig_name=None, x_scale=None, y_scale=None, x_err=None, y_err=None, x_label=None, y_label=None, Binning=False):
def Plot(Path=os.getcwd(), **kwargs):
x = np.linspace(0,20)
y = np.linspace(0,10)
if x_scale is not None:
xs = np.ones_like(x)
x = x/xs
if y_scale is not None:
ys = np.ones_like(y)
y=y/ys
if x_err is not None: #The error is raised here
x_err = np.ones_like(x)
if y_err is not None:
y_err = np.ones_like(y)
if fig_name is None:
fig_name = y_name+x_name+'.png'
#and then I would do the plots
return Plot
Plot_1 = PlotFunc('x', 'y', 'x-y-linspace.png', x_err=None, y_scale=np.ones(50), x_label=r'x', y_label=r'y')
运行ning Plot_1
引发错误“UnboundLocalError: local variable 'x_err' referenced before assignment”,我觉得很奇怪,因为之前的所有变量都没有被检查过。
我是不是做错了什么,或者在 python3 的闭包中可以传递多少个变量有限制吗?我运行python3.6.9
由于您在函数 Plot(Path=os.getcwd(), **kwargs)
中为 x_err
赋值,因此它会隐藏外部作用域中的名称。您可以将变量传递给函数,也可以将变量名称更改为 PlotFunc
和 Plot
.
中的名称不同
def PlotFunc(x_name, y_name, fig_name=None, x_scale=None, y_scale=None, x_err=None, y_err=None, x_label=None, y_label=None, Binning=False):
def Plot(Path=os.getcwd(), **kwargs):
x = np.linspace(0,20)
y = np.linspace(0,10)
if x_scale is not None:
xs = np.ones_like(x)
x = x/xs
if y_scale is not None:
ys = np.ones_like(y)
y=y/ys
if x_err is not None:
x_err_other = np.ones_like(x)
if y_err is not None:
y_err_other = np.ones_like(y)
if fig_name is None:
fig_name_other = y_name+x_name+'.png'
return Plot
Plot_1 = PlotFunc('x', 'y', 'x-y-linspace.png', x_err=None, y_scale=np.ones(50), x_label=r'x', y_label=r'y')
我正在尝试编写一个 closure/nested 函数来绘制许多类似的图。现在似乎并非所有值都被传递,因为我尝试传递的变量之一得到 'UnboundLocalError':
我的代码如下:
def PlotFunc(x_name, y_name, fig_name=None, x_scale=None, y_scale=None, x_err=None, y_err=None, x_label=None, y_label=None, Binning=False):
def Plot(Path=os.getcwd(), **kwargs):
x = np.linspace(0,20)
y = np.linspace(0,10)
if x_scale is not None:
xs = np.ones_like(x)
x = x/xs
if y_scale is not None:
ys = np.ones_like(y)
y=y/ys
if x_err is not None: #The error is raised here
x_err = np.ones_like(x)
if y_err is not None:
y_err = np.ones_like(y)
if fig_name is None:
fig_name = y_name+x_name+'.png'
#and then I would do the plots
return Plot
Plot_1 = PlotFunc('x', 'y', 'x-y-linspace.png', x_err=None, y_scale=np.ones(50), x_label=r'x', y_label=r'y')
运行ning Plot_1
引发错误“UnboundLocalError: local variable 'x_err' referenced before assignment”,我觉得很奇怪,因为之前的所有变量都没有被检查过。
我是不是做错了什么,或者在 python3 的闭包中可以传递多少个变量有限制吗?我运行python3.6.9
由于您在函数 Plot(Path=os.getcwd(), **kwargs)
中为 x_err
赋值,因此它会隐藏外部作用域中的名称。您可以将变量传递给函数,也可以将变量名称更改为 PlotFunc
和 Plot
.
def PlotFunc(x_name, y_name, fig_name=None, x_scale=None, y_scale=None, x_err=None, y_err=None, x_label=None, y_label=None, Binning=False):
def Plot(Path=os.getcwd(), **kwargs):
x = np.linspace(0,20)
y = np.linspace(0,10)
if x_scale is not None:
xs = np.ones_like(x)
x = x/xs
if y_scale is not None:
ys = np.ones_like(y)
y=y/ys
if x_err is not None:
x_err_other = np.ones_like(x)
if y_err is not None:
y_err_other = np.ones_like(y)
if fig_name is None:
fig_name_other = y_name+x_name+'.png'
return Plot
Plot_1 = PlotFunc('x', 'y', 'x-y-linspace.png', x_err=None, y_scale=np.ones(50), x_label=r'x', y_label=r'y')