[Python] How to initialize variables to None/Undefined and compare to other variables in...

Discussão em 'Python' iniciado por Stack, Junho 7, 2025.

  1. Stack

    Stack Membro Participativo

    Basically I have some variables that I don't want to preinitialize:

    originalTime = None
    recentTime = None
    postTime = None

    def DoSomething ( ) :
    if originalTime == None or (postTime - recentTime).seconds > 5 :
    ...


    I get compile error on the if:

    UnboundLocalError: local variable 'originalTime' referenced before assignment


    As you can see, all the variables have different relationship that either has to be set right (time, time + 5, etc) or None at all, but I don't wanna set them to precalculated values when just declaring them to None is easier.

    Any ideas?

    Continue reading...

Compartilhe esta Página