call a variable from one function to another ... - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: call a variable from one function to another ... (/thread-12855.html) |
call a variable from one function to another ... - evilcode1 - Sep-16-2018 hello all ... i need to call this variable ( filename )from function qassam() inside another fileex() .. this is my code : import os def qassam(): filename = "/root/Downloads/scapy.txt" #return filename def fileex(): filename = qassam if os.path.isfile(filename): print("is there") else: print("else ... ") fileex()error :
RE: call a variable from one function to another ... - Windspar - Sep-16-2018 Function variables are local to function. They cannot be shared. options. maybe have function return variable. import os def qassam(): filename = "/root/Downloads/scapy.txt" return filename def fileex(): filename = qassam() if os.path.isfile(filename): print("is there") else: print("else ... ") fileex()Make shared variables import os class ShareVariables: filename = "" def qassam(): ShareVariables.filename = "/root/Downloads/scapy.txt" def fileex(): if os.path.isfile(ShareVariables.filename): print("is there") else: print("else ... ") qassum() fileex()My least favorite. Have global variable import os filename = "" def qassam(): global filename filename = "/root/Downloads/scapy.txt" def fileex(): if os.path.isfile(filename): print("is there") else: print("else ... ") qassum() fileex() RE: call a variable from one function to another ... - evilcode1 - Sep-16-2018 but how its work while its not global ??? i try to use return and its working : import os def qassam(): filename = "/root/Downloads/scapy.txt" return filename def fileex(): filename = qassam() #x = filename print(filename) if os.path.isfile(str(filename)): print("is there") else: print("else ... ") fileex() RE: call a variable from one function to another ... - Windspar - Sep-16-2018 Roughly. Low level stuff. Anytime a function return a variable. It is put on the stack. Function closes. Then stack item is assign to variable. python bytecode. import os import dis def qassam(): filename = "/root/Downloads/scapy.txt" return filename def fileex(): filename = qassam() if os.path.isfile(filename): print("is there") else: print("else ... ") dis.dis(fileex)
RE: call a variable from one function to another ... - gruntfutuk - Sep-16-2018 You do need to call the first function at some point if you want to exploit it later. One approach, rather than returning a value or using globals is to set an attribute: import os def qassam(): qassam.filename = "words.csv" def fileex(): filename = qassam.filename if os.path.isfile(filename): print("is there") else: print("else ... ") qassam() fileex() |