我如何在全局函数内创建列表?

问题描述 投票:0回答:1

在此函数中,我有一个要全局设置的列表,并允许访问该函数之外的内容:

def get_data():
    d = client_socket.recv(BUFSIZ)
    feed = pickle.loads(d)

    number_authorized = []
    for q in feed:
        number_authorized.append(q[0]) 
    number_authorized.sort()

number_authorized列表每次调用函数get_data()时都会更改。我希望全局number_authorized列表根据函数内部的更改而更改。

如何允许在函数外访问列表及其值?每次尝试更改或访问列表的值时都必须声明global number_authorized吗?

如果您能帮助我,我将非常感谢!!! :)

python list global local-variables
1个回答
0
投票

只需声明:

def get_data():
    d = client_socket.recv(BUFSIZ)
    feed = pickle.loads(d)

    global number_authorized #declaration

    number_authorized = []
    for q in feed:
        number_authorized.append(q[0]) 
    number_authorized.sort()
© www.soinside.com 2019 - 2024. All rights reserved.