示例:
# Suppose id(i) == A
i = 10
if some_condition:
i = 11
# How to make sure this 'i' has an id == A instead of B?
# Because at this time my pycharm prompts me that the i above is not used.
当我想声明一个同名的局部变量而不影响上面 i 的值时该怎么办?
或者,有没有办法在python3中强制只赋值而不声明变量,然后在名称不存在时抛出异常提示开发人员?
如果有人提供帮助,我将不胜感激。
我认为,最好的答案是改变变量。但是,如果你仍然想使用它。你可以试试这个
制作一些功能
i = 10
def scope_test():
def do_local():
i = 11
do_local()
print(i)
print(i)
scope_test()
print(i)
i = 10
def scope_test():
def do_local():
i = 11
do_local(outernet)
print(i)
print(i)
scope_test() 打印(一)