Function variable scope in python

前端 未结 3 758
无人共我
无人共我 2021-01-29 01:04

let\'s say we have two functions:

def ftpConnect(): 
    ftp = FTP(\'server\')
    ftp.login()
    ftp.cwd(\'/path\')

def getFileList():
    ftpConnect()
    fi         


        
3条回答
  •  萌比男神i
    2021-01-29 01:16

    Return ftp from ftpConnect() and assign the return value to a variable named ftp:

    def ftpConnect(): 
        ftp = FTP('server')
        ftp.login()
        ftp.cwd('/path')
        return ftp         #return ftp from here
    
    def getFileList():
        ftp = ftpConnect() # assign the returned value from the
                           # function call to a variable
        files = ftp.nlst()
        print(ftp.nlst())
    

提交回复
热议问题