Function variable scope in python
问题 let's say we have two functions: def ftpConnect(): ftp = FTP('server') ftp.login() ftp.cwd('/path') def getFileList(): ftpConnect() files = ftp.nlst() print(files) If I call the getFileList() function it won't work because it doesn't know the ftp var. I know that if I declare the ftp variable inside ftpConnect() function as global it will work, but I was wondering if there is a better / more elegant way of doing it. 回答1: In my opinion, the most elegant solution would be to make a FTP-class,