"""演示 LEGB、global 与局部屏蔽。""" from __future__ import annotations count = 0 def bump() -> int: """修改模块级 count。""" global count count += 1 return count def read_outer() -> int: """闭包读取外层变量(不需要 nonlocal)。""" value = 10 def inner() -> int: return value return inner() def local_shadow(value: int) -> int: """局部变量与模块级同名时,函数内优先局部。""" count = value return count def lookup_table() -> dict[str, int]: return {"bump": bump(), "read_outer": read_outer(), "local_shadow": local_shadow(99)} def main() -> None: for key, value in lookup_table().items(): print(f"{key}: {value}") if __name__ == "__main__": main()