如何将 Python 变量设置为“未定义”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23828242/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 03:28:38  来源:igfitidea点击:

How to set a Python variable to 'undefined'?

pythonvariablespython-3.x

提问by Duke Dougal

In Python 3, I have a global variable which starts as "undefined".

在 Python 3 中,我有一个以“未定义”开头的全局变量。

I then set it to something.

然后我将它设置为一些东西。

Is there a way to return that variable to a state of "undefined"?

有没有办法将该变量返回到“未定义”状态?

@martijnpieters

@martijnpieters

EDIT - this shows how a global variable starts in a state of undefined.

编辑 - 这显示了全局变量如何以未定义的状态启动。

Python 2.7.5+ (default, Feb 27 2014, 19:37:08) 
[GCC 4.8.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> x
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'x' is not defined
>>> global x
>>> x
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'x' is not defined
>>> 

采纳答案by Sven Marnach

You can delete a global name xusing

您可以删除全局名称x使用

del x

Python doesn't have "variables" in the sense C or Java have. In Python, a variable is just a tag you can apply to any object, as opposed to a name refencing some fixed memory location.

Python 没有 C 或 Java 那样的“变量”。在 Python 中,变量只是一个可以应用于任何对象的标记,而不是引用某个固定内存位置的名称。

Deleting doesn't necessarily remove the object the name pointed to.

删除不一定会删除名称指向的对象。

回答by justengel

You probably want to set it to None.

您可能希望将其设置为无。

variable = None

Check if variable is "defined"

检查变量是否“已定义”

is_defined = variable is not None

You could delete the variable, but it is not really pythonic.

您可以删除该变量,但它并不是真正的 Pythonic。

variable = 1
del variable
try:
    print(variable)
except (NameError, AttributeError):
    # AttributeError if you are using "del obj.variable" and "print(obj.variable)"
    print('variable does not exist')

Having to catch a NameError is not very conventional, so setting the variable to None is typically preferred.

必须捕获 NameError 并不是很传统,因此通常首选将变量设置为 None。

回答by Guillaume

You can also define your var x as None

您还可以将 var x 定义为 None

x = None

回答by damienfrancois

If you want to be able to test its 'undefined state', you should set it to None :

如果您希望能够测试其“未定义状态”,则应将其设置为 None :

variable = None

and test with

并测试

if variable is None: 

If you want to clean stuff, you can delete it, del variablebut that should be task of the garbage collector.

如果你想清理东西,你可以删除它,del variable但这应该是垃圾收集器的任务。

回答by Stefan van den Akker

In light of the OP's comments:

根据 OP 的评论:

# check if the variable is undefined
try:
    x
# if it is undefined, initialize it
except NameError:
    x = 1

And like the rest said, you can delete a defined variable using the delkeyword.

就像其他人说的那样,您可以使用del关键字删除定义的变量。