The problem is this line:
def __init__(self, lst=[], intg=0):
You should not use the list as the default argument. The first time __init__ is called without lst , the specified Python interpreter will define an empty list [] . Subsequent function calls will work in the same list if lst not specified without declaring a new list. This causes strange problems.
Instead, you should use the default value of None and add a check at the beginning of the function:
def __init__(self, lst=None, intg=0): if lst is None: lst = []
See this post for more details. Quote:
The default arguments are evaluated at the time the function is defined, so they are constant between calls. This has some interesting (and confusing) side effects. Example:
>>> def foo(d=[]): ... d.append('a') ... return d
If you have not tried this before, you probably expect foo to always return ['a'] : it should start with an empty list, add 'a' to it and return it. Here is what he actually does:
>>> foo() ['a'] >>> foo() ['a', 'a'] >>> foo() ['a', 'a', 'a']
This is because the default value for d is allocated when the function and not when it is called. every time the function calls, the value is still hanging from the last call. It gets even weirder if you throw the threads into the mix. If two different threads perform functions at the same time, and one of them changes the default argument, they will both see the change.
Of course, all this is true only if the default argument value is a mutable type. If we change foo to is defined as
>>> def foo2(d=0): ... d += 1 ... return d
then it will always return 1. (The difference here is that in foo2 , the variable d reassigned, while in foo its value has been changed.)