python 作用于的问题(超级有意思)


str_ok = 'jack'
def cs_first():
    try:
        print("111=", str_ok)
        print("222=", 4 / 0)
        # try:
        #     print("1212=", str_ok)
        #     pass
        # except:
        #     pass
        # finally:
        #     print("1414=", str_ok)
        #     pass
        # pass
    except:
        # str_ok = 'lucy'
        print("222=", str_ok)
    finally:
        # str_ok = 'uuuu'
        print("333=", str_ok)


cs_first()

总结:分步,打开finally,except里面的str_ok 变量,看看作用域 或 作用。

-------------------------------------------------------------  下面是网友给出飞分析答案

1、

解决Python报错:local variable 'xxx' referenced before assignment(引) - 菜鸡一枚 - 博客园https://www.cnblogs.com/yymn/p/11104440.html

2、

python 全局变量引用与修改 - 菜鸡一枚 - 博客园https://www.cnblogs.com/yymn/p/11299805.htmlpython 全局变量引用与修改 - yanfengt - 博客园https://www.cnblogs.com/yanfengt/p/6305542.html

3、

一个网友学习python的知识点:
https://blog.csdn.net/langb2014/category_2495401.htmlhttps://blog.csdn.net/langb2014/category_2495401.html

4、

https://www.jb51.net/article/108952.htmicon-default.png?t=L9C2https://www.jb51.net/article/108952.htm

猜你喜欢

转载自blog.csdn.net/weixin_54217632/article/details/120895483
今日推荐