简体   繁体   中英

Python - Variable from function to global

I got a short script that is supposed to simulate a stack. The script consists of two functions, one for the push and one for the pop operation. The functions are called once per operation.

My issue is that apparently for the pop-function the global variable for the stack does not get updated by the output of the pop-function and I just don't get why that is, as the push-function works fine and to me it looks like the pop-function works in an analogous fashion.

I have looked into other threads that talk about how to instantiate global variables with the return values of the functions that got called with the viable as the parameter, but apparently I am missing something.

Could some body point me to the issue with my script and what my misconception about passing values between local and global variables is?

So this is my script:

l = []

def push(l_in,a):
    l_in.append(a)
    l_out = l_in
    print l_out
    return l_out


def pop(l_in):
    length = len(l_in)
    if length == 0:
        print []
    else:
        l_in = l_in[0:length-1]
    print l_in
    return l_in

But for this request:

push(l,'a')
push(l,'b')
push(l,'c')
pop(l)
pop(l)
pop(l)

I get this output:

['a']
['a', 'b']
['a', 'b', 'c']
['a', 'b']
['a', 'b']
['a', 'b']

So push is working, pop not. But I don't undertsand what the difference is in terms of passing values from the function to the global variable.

Thanks

You are not handling a globals. You are passing in a local l_in , but in pop() you then rebind the local to a new list object:

l_in = l_in[0:length-1]

l_in was a reference to the same mutable list object the global l is a reference to.

Lists have a .pop() method for just this use-case, removing the last element. It'll mutate the list object directly:

l_in.pop()

The alternative would be to assign to the sliced list, that'll replace all indices in the list with the new list indices:

l_in[:] = l_in[0:length-1]

You don't even need length here, as negative indices count from the end. The 0 is also optional, leaving that entry empty would also work:

l_in[:] = l_in[:-1]

A final option is to delete a specific index using del :

del l_in[-1]

which would also remove the last element.

Push is using an operation that mutates the list append , but pop is assigning to the local variable l_in = l_in[0:length-1] . That creates a new list and assigns it to the local variable. You need to mutate for that operation as well. For example del l_in[-1] .

If you don't want to use the pop() method that is provided by Python because you are experimenting and wanted to write your own, then the equivalent function could be:

def pop(l_in):
    if len(l_in) == 0:
        return []
    else:
        ret = l_in[-1]
        del l_in[-1]
    return ret

I'm also assuming that you want your pop() function to return the value popped and not the whole list.

You're editing the list L and therefore you should bind it to your functions returns

l = push(l,'a')
l = push(l,'b')
l = push(l,'c')
l = pop(l)
l = pop(l)
l = pop(l)

this will ensure that L is always updated after the function call. cheers

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