Python Forum
call a variable from one function to another ...
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
call a variable from one function to another ...
#2
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()
99 percent of computer problems exists between chair and keyboard.
Reply


Messages In This Thread
RE: call a variable from one function to another ... - by Windspar - Sep-16-2018, 09:47 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Variable for the value element in the index function?? Learner1 8 729 Jan-20-2024, 09:20 PM
Last Post: Learner1
  Variable is not defined error when trying to use my custom function code fnafgamer239 4 639 Nov-23-2023, 02:53 PM
Last Post: rob101
  Printing the variable from defined function jws 7 1,424 Sep-03-2023, 03:22 PM
Last Post: deanhystad
  Function parameter not writing to variable Karp 5 1,019 Aug-07-2023, 05:58 PM
Last Post: Karp
  How do I call sys.argv list inside a function, from the CLI? billykid999 3 840 May-02-2023, 08:40 AM
Last Post: Gribouillis
  Retrieve variable from function labgoggles 2 1,079 Jul-01-2022, 07:23 PM
Last Post: labgoggles
  Cant transfer a variable onto another function KEIKAS 5 1,947 Feb-09-2022, 10:17 PM
Last Post: deanhystad
  how to call an object in another function in Maya bstout 0 2,110 Apr-05-2021, 07:12 PM
Last Post: bstout
  In this function y initially has no value, but a call to foo() gives no error. Why? Pedroski55 8 3,567 Dec-19-2020, 07:30 AM
Last Post: ndc85430
  Please explain uncommon way of declaring and using variable [function.variable] esphi 4 2,383 Nov-07-2020, 08:59 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020