简体   繁体   中英

Def and Return Function in python

I'm having some problems with the def and return function in Python. 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 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). 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 = subprogram(ssh, x_off, y_off, data_array, i, j)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM