Python Forum
Calling list from previous function
Thread Rating:
  • 2 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Calling list from previous function
#1
I am writing a program using python w/ tkinter. The GUI is three buttons. Two of them are to be used for inputting a text file, then converting it to a list. I got that part. Next, I need to call the lists (bbb in first function, ddd in second function) to my third function. How would I "summons" them, so to speak, in the code below? (in third function titled button_3) --- Thanks in advance!
     def button_1(self):
        InsertedTextFile =  filedialog.askopenfilename()
        with open (InsertedTextFile, "r") as aaa:
            aaa = csv.reader(aaa)         
            bbb = list(aaa)
            print(bbb)
               

    def button_2(self):
        InsertedTextFile =  filedialog.askopenfilename()
        with open (InsertedTextFile, "r") as ccc:
            ccc = csv.reader(ccc)         
            ddd = list(ccc)
            print(ddd)
            
    def button_3(self):
        print (ddd+bbb)

Nevermind. If anyone sees in future, you do this by putting <<<Global ddd>>> and <<<Global bbb>>> above InsertedTextFile line in each function. (See below)

 def button_1(self):
    Global bbb
    InsertedTextFile =  filedialog.askopenfilename()
    with open (InsertedTextFile, "r") as aaa:
        aaa = csv.reader(aaa)         
        bbb = list(aaa)
        print(bbb)
                
 
def button_2(self):
    Global ddd
    InsertedTextFile =  filedialog.askopenfilename()
    with open (InsertedTextFile, "r") as ccc:
        ccc = csv.reader(ccc)         
        ddd = list(ccc)
        print(ddd)
Reply
#2
globals usage is considered bad style for quite a few reasons.

See: http://wiki.c2.com/?GlobalVariablesAreBad
https://softwareengineering.stackexchang...te-so-evil
https://en.wikipedia.org/wiki/Side_effec...r_science)
I rarely (almost never) use them.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  calling external function with arguments Wimpy_Wellington 7 1,344 Jul-05-2023, 06:33 PM
Last Post: deanhystad
  Calling a function (which accesses a library) from another file mouse9095 4 766 Jun-07-2023, 08:55 PM
Last Post: deanhystad
Sad Iterate randint() multiple times when calling a function Jake123 2 1,979 Feb-15-2022, 10:56 PM
Last Post: deanhystad
  Calling a class from a function jc4d 5 1,754 Dec-17-2021, 09:04 PM
Last Post: ndc85430
  [Solved] TypeError when calling function Laplace12 2 2,830 Jun-16-2021, 02:46 PM
Last Post: Laplace12
  calling a function and argument in an input phillup7 3 2,551 Oct-25-2020, 02:12 PM
Last Post: jefsummers
  Function Recognises Variable Without Arguments Or Global Variable Calling. OJGeorge4 1 2,205 Apr-06-2020, 09:14 AM
Last Post: bowlofred
  Calling DLL function OptoBruh 0 1,559 Nov-15-2019, 11:51 PM
Last Post: OptoBruh
  Help with calling list from user input farispython 5 2,748 Nov-03-2019, 03:13 PM
Last Post: Gribouillis
  I created a function that generate a list but the list is empty in a new .py file mrhopeedu 2 2,246 Oct-12-2019, 08:02 PM
Last Post: mrhopeedu

Forum Jump:

User Panel Messages

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