python-guide icon indicating copy to clipboard operation
python-guide copied to clipboard

Add new gotcha

Open acampove opened this issue 8 months ago • 0 comments

Hi,

While running tests on my code I was doing something like:

class Data:
    mylist = [1,2,3,4]


def test_function():
    local_list = Data.mylist
    local_list+=new_list

    # Do stuff with 

and there are multiple tests using mylist. This is going to modify the list instead of creating a new one, an unexpected behavior of +=. This will let one test talk to the tests happening later. The safer approach is:

class Data:
    mylist = [1,2,3,4]


def test_function():
    local_list =new_list + Data.mylist

    # Do stuff with 

here + will copy the list. Could you please add this case?

Cheers.

acampove avatar May 18 '25 15:05 acampove