-
Notifications
You must be signed in to change notification settings - Fork 481
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
c01/c01_19 #56
Comments
def a(): print(a()) 来解释一下这个 |
试着执行了楼上的代码,异常信息如下 In [9]: print(a())
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[5], line 4, in a()
3 a = '1'
----> 4 b = a * 10 - 5
5 return b
TypeError: unsupported operand type(s) for -: 'str' and 'int'
During handling of the above exception, another exception occurred:
UnboundLocalError Traceback (most recent call last)
Cell In[9], line 1
----> 1 print(a())
Cell In[5], line 7, in a()
5 return b
6 finally:
----> 7 return b
UnboundLocalError: local variable 'b' referenced before assignment
但是实际上是, 在一楼的代码上修改一下,就能正常执行 finally 语句了 def a():
try:
a = '1'
b = 'finally will get this value'
b = a * 10 - 5
return b
finally:
return b 执行结果 In [11]: a()
Out[11]: finally will get this value 好像这个还涉及到 “LEGBA” 的变量查找顺序的知识点 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
1.19 return不一定都是函数的终点 — Python黑魔法手册 1.0.0 documentation
https://magic.iswbm.com/c01/c01_19.html
The text was updated successfully, but these errors were encountered: