Is it possible only to declare a variable without assigning any value in Python?

后端 未结 14 1866
暖寄归人
暖寄归人 2020-11-30 17:26

Is it possible to declare a variable in Python, like so?:

var

so that it initialized to None? It seems like Python allows this, but as soon

14条回答
  •  长情又很酷
    2020-11-30 17:41

    Well, if you want to check if a variable is defined or not then why not check if its in the locals() or globals() arrays? Your code rewritten:

    for index in sequence:
       if 'value' not in globals() and conditionMet:
           value = index
           break
    

    If it's a local variable you are looking for then replace globals() with locals().

提交回复
热议问题