简体   繁体   中英

Python - Using list outside function

Following on from my previous question: Python - different variable with each function run

I can now run the function multiple times and append to a list which is great. The problem now, is accessing the list outside of the function.

def TwoPic():
    run = 0
    Lats = []
    Longs = []
    while run < 2:
        run = run + 1
        print ("Start func TwoPic")
        oneMorePic()
        Lats.append(finalLatSecondPhoto)
        Longs.append(finalLongSecondPhoto)

        print '\n\nYour coordinates: ' + rippedLatitudeTwo + ',-' + rippedLongitudeTwo
        print "Below are the 2 new lats and longs in a list"
        print Lats
        print Longs
        root.quit()
    return Lats, Longs

root = Tk() 

label = Label(root, text="Choose Iterations", font=('Calibri', 12))
button1 = Button(root, text = "0 Pics", command = Quit)
button2 = Button(root, text = "1 Pic", command = OnePic)
button3 = Button(root, text = "2 Pics", command = TwoPic)

label.pack()
button1.pack()
button2.pack()
button3.pack()
root.mainloop() 

print "List: "

print Lats, Longs # Should print my list....I'm now outside of the function

Error:

Your coordinates: 43 48.54,-1 24.58
Below are the 2 new lats and longs in a list
[43.809, 43.809]
[1.409, 1.409]
List:
Traceback (most recent call last):
  File "simonsScript.py", line 447, in <module>
    print Lats, Longs
NameError: name 'Lats' is not defined

Why is it not letting me print the list outside of the function?

Your Lats variable is not in scope when you try to print it. Try to define it outside the function ( note : it's rarely a good idea to have "global" variables; I assume you know what you're doing, and that you have no other sane choice).

Lats = []

def TwoPic():
    run = 0
    [...]

print Lats

Also, it's not good practice to define variables with a capital letter. Of course it depends on your naming convention, but it might be easier to read other people's code in the future if you get used to common practices.

This means:

lats = []
def two_pic():
    run = 0
    [...]

print lats,long..

If you want to know more about good coding practices for python (not necessarily related to naming variables) search for "pep8" or look at this link

Why is it not letting me print the list outside of the function?

Because it's a local name that only exists within the function (and FIY your Lats and Longs list only lives for the duration of the function's execution.

Lats and Longs defined inside TwoPic() scope they are not global, you are trying to access variable that is not defined.

Change you code to first define them by the function you made and then print their value.

Lats, Longs = TwoPic() 
print "Lists:", Lats, Longs

what you do is absurd…

you create a Button such as when you click on it you get inside TwoPic() :

button3 = Button(root, text = "2 Pics", command = TwoPic)

though you want to get the data inside Lats and Longs before they are defined.

You should instead be using the Lats and Longs from within TwoPic , once you have defined them. And for example show them in a Label , or as you already do, inside print statements.

Another solution:

class HandleLongLats:
    def __init__(self):
        self.longs = []
        self.lats = []

    def twoPic(self):
        run = 0
        while run < 2:
            run = run + 1
            print ("Start func TwoPic")
            oneMorePic()
            self.lats.append(finalLatSecondPhoto)
            self.longs.append(finalLongSecondPhoto)

            print '\n\nYour coordinates: ' + rippedLatitudeTwo + ',-' + rippedLongitudeTwo
            print "Below are the 2 new lats and longs in a list"
            print self.lats
            print self.longs
            root.quit()
        self.showList()

    def showUI(self):
        root = Tk() 

        label = Label(root, text="Choose Iterations", font=('Calibri', 12))
        button1 = Button(root, text = "0 Pics", command = Quit)
        button2 = Button(root, text = "1 Pic", command = onePic)
        button3 = Button(root, text = "2 Pics", command = self.twoPic)

        label.pack()
        button1.pack()
        button2.pack()
        button3.pack()
        root.mainloop() 

    def showList(self):  
        print "List: "
        print self.lats, self.longs # Should print my list....I'm now outside of the function

Here the idea is to make lats and longs members of a class, so you can use them from within another function.

You try to reach variables which are out of the scope at the place where you try to print them. In your code you can only access those variables inside the function. There are 3 solutions to your problem.

1: make them gloabl like:

def TwoPic():
    run = 0
    global Lats = []
    global Longs = []
    [...]

2: define the variables outside the function

Lats = []
Longs = []
def TwoPic():
    run = 0 
    [...]

3: return them from the function

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM