【python】global

发布时间:2024年01月05日

No need for ‘global’ if you’re just reading the global variable

my_global_var = 0

def read_global_var():
    print(my_global_var)  # This is fine, we're just reading the value

Need to declare ‘global’ if you’re modifying the global variable

def modify_global_var():
    global my_global_var  # Necessary to modify the global variable
    my_global_var += 1
文章来源:https://blog.csdn.net/qq_18846849/article/details/135404347
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。