[英]Def and Return Function in python
I'm having some problems with the def and return function in Python.我在 Python 中的 def 和 return 函数有一些问题。 On the top of my program I've defined:
在我的程序顶部,我定义了:
from subprogram import subprogram
then I've defined the def in which I've included the values I wanted to be returned:然后我定义了包含我想要返回的值的 def:
def subprogram(ssh, x_off, y_off, data_array, i, j):
if j==1:
print('shutdonw X')
# Run command.
ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command(x_off)
var_colonna_1 = data_array[i][j]
return var_colonna_1
print(var_colonna_1)
if j==2:
print('shutdown Y')
# Run command.
ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command(y_off)
var_colonna_2 = data_array[i][j]
return var_colonna_2
this is then called in the main program as:然后在主程序中将其调用为:
for j in range(5, lunghezza_colonna):
if data_array[i][j] == 'V':
subprogram(ssh, x_off, y_off, data_array, i, j)
print(var_colonna_1)
I was expecting that every time the subprogram
is called, it returns the var_colonna_1
or var_colonna_2
, but the values I see when I print var_colonna_1
is always 0 even if, internally the other commands are execute (so X and Y are set in shut down).我期待每次调用
subprogram
时,它都会返回var_colonna_1
或var_colonna_2
,但是我在打印var_colonna_1
时看到的值始终为 0,即使在内部执行其他命令(因此 X 和 Y 设置为关闭) . Can you help me?你能帮助我吗? I don't see my coding mistake.
我没有看到我的编码错误。
The function doesn't return the variable, only the value on it.该函数不返回变量,只返回它的值。
If you want to get the returned value on var_colonna_1, you should asign it, as Sayse said, you should do:如果你想在 var_colonna_1 上获得返回值,你应该分配它,正如 Sayse 所说,你应该这样做:
var_colonna_1 = subprogram(ssh, x_off, y_off, data_array, i, j)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.