[Python] Python Global Variables - Not Defined? [duplicate]

Discussão em 'Python' iniciado por Stack, Setembro 5, 2025.

  1. Stack

    Stack Membro Participativo

    I'm running into an issue where a global variable isn't "remembered" after it's modified in 2 different functions. The variable df is supposed to be a data frame, and it doesn't point to anything until the user loads in the right file. This is similar to something I have (using pandas and tkinter):

    global df

    class World:

    def __init__(self, master):
    df = None
    ....

    def load(self):
    ....
    df = pd.read_csv(filepath)

    def save(self):
    ....
    df = df.append(...)


    save() is always called after load(). Thing is, when I call save(), I get the error that "df is not defined." I thought df got its initial assignment in init(), and then got "updated" in load()? What am I doing wrong here?

    Continue reading...

Compartilhe esta Página