简体   繁体   English

变量是否已定义或会引发异常

[英]Is the variable defined or will it raise an exception

Reading this book and am trying to understand something.阅读这本书,并试图理解一些东西。 Within this try and except clause i was told that an exception would be raised because variable C is not defined but it looks like the variable is defined.在这个 try and except 子句中,我被告知会引发异常,因为变量 C 未定义,但看起来变量已定义。 Is it because of the try/except clause?是因为 try/except 子句吗? Seems like C's value would be "I will never get defined."似乎 C 的价值是“我永远不会被定义”。

try:
    10 / 0
    c = "I will never get defined."
except ZeroDivisionError:
    print(c)

The reason c will never be defined is because 10/0 will raise an error.永远不会定义c的原因是因为10/0会引发错误。 With an error, the try block can't continue, and will jump into the except block.出现错误时,try 块无法继续,将跳转到 except 块。 And in the end, c haven't been defined.最后, c还没有被定义。

Here's what happens step by step:这是一步一步发生的事情:

  • Python enters the try.. except block Python进入try.. except
  • 10 / 0 gets executed and raises a ZeroDivisionError exception 10 / 0被执行并引发ZeroDivisionError异常
  • Python jumps to the except part of the block, skipping the c = instruction Python 跳转到块的except部分,跳过c =指令
  • print(c) gets executed, but since the definition of c was skipped, a new exception is raised print(c)被执行,但由于c的定义被跳过,引发了一个新的异常

At the module level, variables don't exist until their first assignment.在模块级别,变量在第一次赋值之前不存在。

c = "I will never get defined."

creates the variable "c" in the module's namespace and assigns the string.在模块的命名空间中创建变量“c”并分配字符串。 Before that assignment, "c" simply does not exist in the module namespace.在分配之前,模块命名空间中根本不存在“c”。 If you print the namespace variables before the error, there is no "c".如果在错误之前打印命名空间变量,则没有“c”。 I added "foo" to demonstrate a variable that has been assigned.我添加了“foo”来演示已分配的变量。

try:
    foo = "I am defined!"
    print("Existing variables:", sorted(globals().keys()))
    10 / 0
    c = "I will never get defined."
except ZeroDivisionError:
    print(c)

Output Output

Existing variables: ['__annotations__', '__builtins__', '__cached__', '__doc__',
'__file__', '__loader__', '__name__', '__package__',
'__spec__', 'foo']

Traceback (most recent call last):
  File "m.py", line 4, in <module>
    10 / 0
ZeroDivisionError: division by zero

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "m.py", line 7, in <module>
    print(c)
NameError: name 'c' is not defined

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 python在变量内引发异常 - python raise exception inside a variable DRF-如果任何定义的字段为None,则引发Exception - DRF - Raise Exception if any defined field is None 引发异常或引发Exception() - raise Exception or raise Exception() 在 Python 中引发用户定义的异常,仅在条件下终止程序 - Raise user defined Exception in Python that only terminates program on condition 如果在 scope 中定义了该名称的迭代,未定义变量不会引发错误 - undefined variable does not raise error if in scope of iteration with that name defined 如果语句不引发异常,则为变量赋值的 Pythonic 方法 - Pythonic way of assigning value to a variable if statement does not raise exception 为什么对全局变量的错误赋值会提前引发异常? - Why does incorrect assignment to a global variable raise exception early? 如果变量在被另一个函数“初始化”之前被访问,则引发异常 - Raise an exception if a variable is accessed before being "initialized" by another function 如果将用户定义的类传递给错误形状的数组,请使用python引发异常 - use python to raise an exception if a user-defined class is passed an array of the incorrect shape django在某处引发异常 - django raise the exception somewhere
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM