简体   繁体   中英

Python global variable doesnt change

Example code:

podbor_dict = {}
def change(lst, sz):
    lst_r = [lst[i:i+sz] for i in range(0, len(lst), sz)]
    return lst_r
def qq():
    global podbor_dict
    if podbor_dict.get(
            'podbor_'):
        podbor = podbor_dict[
            'podbor_']
        podbor.clear()
    else:
        podbor_dict.update(
            {'podbor_': []})
        podbor = podbor_dict[
            'podbor_']
    podbor = ['a', 'b', 'c', 'd', 'a', 'b', 'c', 'd', 'a', 'b', 'c', 'd', 'a', 'b', 'c', 'd', 'a', 'b', 'c', 'd', 'a', 'b', 'c', 'd', 'a', 'b', 'c', 'd', ]
    podbor = change(podbor, 10)
    print(podbor)


def zz():
    global podbor_dict
    podbor = podbor_dict[
        'podbor_']
    print(podbor)
    print(podbor_dict)


qq()
zz()

Function 'change' adds every ten elements to a new array.

Expected output:

[['a', 'b', 'c', 'd', 'a', 'b', 'c', 'd', 'a', 'b'], ['c', 'd', 'a', 'b', 'c', 'd', 'a', 'b', 'c', 'd'], ['a', 'b', 'c', 'd', 'a', 'b', 'c', 'd']]
[['a', 'b', 'c', 'd', 'a', 'b', 'c', 'd', 'a', 'b'], ['c', 'd', 'a', 'b', 'c', 'd', 'a', 'b', 'c', 'd'], ['a', 'b', 'c', 'd', 'a', 'b', 'c', 'd']]
{'podbor_': [['a', 'b', 'c', 'd', 'a', 'b', 'c', 'd', 'a', 'b'], ['c', 'd', 'a', 'b', 'c', 'd', 'a', 'b', 'c', 'd'], ['a', 'b', 'c', 'd', 'a', 'b', 'c', 'd']]}

Real output:

[['a', 'b', 'c', 'd', 'a', 'b', 'c', 'd', 'a', 'b'], ['c', 'd', 'a', 'b', 'c', 'd', 'a', 'b', 'c', 'd'], ['a', 'b', 'c', 'd', 'a', 'b', 'c', 'd']]
[]
{'podbor_': []}

Several times changed function 'change' but nothing helped. Really looking forward your answer.

This is because you don't actually adding anything to it except for the 'podbor_' with empty list as value. Change the else block to

else:
    podbor = ['a', 'b', 'c', 'd', 'a', 'b', 'c', 'd', 'a', 'b', 'c', 'd', 'a', 'b', 'c', 'd', 'a', 'b', 'c', 'd', 'a', 'b', 'c', 'd', 'a', 'b', 'c', 'd']
    podbor = change(podbor, 10)
    podbor_dict.update({'podbor_': podbor})
    podbor = podbor_dict['podbor_']

# Output: [['a', 'b', 'c', 'd', 'a', 'b', 'c', 'd', 'a', 'b'], ['c', 'd', 'a', 'b', 'c', 'd', 'a', 'b', 'c', 'd'], ['a', 'b', 'c', 'd', 'a', 'b', 'c', 'd']]
#         [['a', 'b', 'c', 'd', 'a', 'b', 'c', 'd', 'a', 'b'], ['c', 'd', 'a', 'b', 'c', 'd', 'a', 'b', 'c', 'd'], ['a', 'b', 'c', 'd', 'a', 'b', 'c', 'd']]
#         {'podbor_': [['a', 'b', 'c', 'd', 'a', 'b', 'c', 'd', 'a', 'b'], ['c', 'd', 'a', 'b', 'c', 'd', 'a', 'b', 'c', 'd'], ['a', 'b', 'c', 'd', 'a', 'b', 'c', 'd']]}

Hey I found a solution

podbor_dict = {}
def change(lst, sz):
    lst_r = [lst[i:i+sz] for i in range(0, len(lst), sz)]
    return lst_r
def qq():
    global podbor_dict
    global podbor
    if podbor_dict.get(
            'podbor_'):
        podbor = podbor_dict[
            'podbor_']
        podbor.clear()
    else:
        podbor_dict.update(
            {'podbor_': []})
        podbor = podbor_dict[
            'podbor_']
    podbor = ['a', 'b', 'c', 'd', 'a', 'b', 'c', 'd', 'a', 'b', 'c', 'd', 'a', 'b', 'c', 'd', 'a', 'b', 'c', 'd', 'a', 'b', 'c', 'd', 'a', 'b', 'c', 'd', ]
    podbor = change(podbor, 10)
    print("fda" , podbor)


def zz():
    global podbor_dict 
    global podbor
    podbor_dict = {'podbor_': podbor}
    print(podbor)
    print(podbor_dict)


qq()
zz()

I made the variable podbor global and with the building from the dictionary you had an error

podbor_dict = {}
def change(lst, sz):
    lst_r = [lst[i:i+sz] for i in range(0, len(lst), sz)]
    return lst_r
def qq():
    global podbor_dict
    if podbor_dict.get(
            'podbor_'):
        podbor_dict['podbor_'].clear()
        # i also removed local variable declarations as they will be dropped at the end of the block anyway
    else:
        podbor_dict.update(
            {'podbor_': []})
    # we can say that if you assing value of dictionary to a variable, it will keep a reference to the list
    # fi we then assign to this variable, we do not change a value in dictionary, but only the reference
    # it is holding, thus dirrect assignment is needed   
    podbor = ['a', 'b', 'c', 'd', 'a', 'b', 'c', 'd', 'a', 'b', 'c', 'd', 'a', 'b', 'c', 'd', 'a', 'b', 'c', 'd', 'a', 'b', 'c', 'd', 'a', 'b', 'c', 'd', ]
    podbor_dict["podbor_"] = change(podbor, 10)
    print(podbor_dict["podbor_"])


def zz():
    global podbor_dict
    podbor = podbor_dict[
        'podbor_']
    print(podbor)
    print(podbor_dict)

qq()
zz()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM