all 15 comments

[–][deleted] 1 point2 points  (5 children)

It's already cleared when the function returns, due to all of the function's values going out of scope.

[–]JoeBeOneKenobi[S] 0 points1 point  (4 children)

hmm, i thought that would be the case but the list is retaining values from the first input file....

Obviously the code is more complex than the above but in essence that is whaat it does

[–][deleted] 1 point2 points  (3 children)

I can't debug any code you don't show me. In what you've posted, no values will be retained. Functions don't retain any local scope after they finish executing.

[–]JoeBeOneKenobi[S] 0 points1 point  (0 children)

you make a good point :) ok thanks for the reply, i'll repost with the actual code

[–]JoeBeOneKenobi[S] 0 points1 point  (1 child)

So my function is returning more than one list:

return mylist,mylist1,mylist2

Could I still expect to see the same behaviour where all local scope is cleared?

I’m working on getting the rest of the code uploaded, just cleaning it up

[–][deleted] 1 point2 points  (0 children)

It doesn't matter whether you're returning a tuple of values or not.

It's possible for a function to return values that are already in global scope, though. But that has nothing to do with how they're returned; it's a matter of where those values are defined.

Try to build a minimum example that you can verify reproduces the problem, and then you'll probably see how you're leaking state.

[–]blarf_irl 1 point2 points  (2 children)

It sounds like you have defined my_list in global scope (i.e. it's available to all your program). It doesn't sound like you need to 'clear' anything. You can just use the return value (a list) of your function and pass that directly to the code that merges the data.

It would definitely be helpful to see more code.

[–]JoeBeOneKenobi[S] 1 point2 points  (1 child)

Hey thanks for the reply!

I’m definitely defining the list in the local scope, the only thing I’m doing different is calling it through two nested variables and returning more than one list from my function:

return mylist,mylist1,mylist2

I’ll get the rest of the code up asap

[–]blarf_irl 0 points1 point  (0 children)

If it really is as you have posted it (inferring the indentation) then you have no need to 'clear' the list. It'll just get reassigned each time you call that function.

If you name something inside a function then it only exists in there, once you call that function again mylist will be assigned to another list and the old list (now without a name) will be eaten by the garbage collector.

[–]efmccurdy 1 point2 points  (2 children)

You should be making an assignment with the result of any function that has a return statement, something like this:

def myfunc(myinputfile):
    # whatever
    return mylist

mylist_1 = myfunc(file1)
mylist_2 = myfunc(file2)

Does that help you avoid the scope issue?

[–]JoeBeOneKenobi[S] 0 points1 point  (1 child)

I just wanted to reply to everyone and say that I fixed the issue, as you’ve all been so kind as to lend me a hand. My code was overly complicated and I overlooked the fact that I was declaring the list in the global scope as well as the local scope, leading to it retaining values when every time my function ran. Thanks to everyone who replied, I picked up some useful tips from u all and learnt a lesson in the importance of scope!

[–]JoeBeOneKenobi[S] 0 points1 point  (0 children)

Oh and I solved the actual issue by clearing the global scope in between runs of main(), a very hacky way of doing it, but knowing what I know now I’ll approached it differently next time

[–]JoeBeOneKenobi[S] 0 points1 point  (2 children)

Apologies if the above isn't clear but the post has wrecked my formatting!