How to isolate a constant?

Alan Meyer ameyer2 at yahoo.com
Tue Oct 25 15:50:59 EDT 2011


On 10/22/2011 8:46 PM, MRAB wrote:
> On 23/10/2011 01:26, Gnarlodious wrote:
>> Say this:
>>
>> class tester():
>> _someList = [0, 1]
>> def __call__(self):
>> someList = self._someList
>> someList += "X"
>> return someList
>>
>> test = tester()
>>
>> But guess what, every call adds to the variable that I am trying to
>> copy each time:
>> test()
>>> [0, 1, 'X']
>> test()
>>> [0, 1, 'X', 'X']
...
> Python will copy something only when you tell it to copy. A simple way
> of copying a list is to slice it:
>
> someList = self._someList[:]

And another simple way:

     ...
     someList = list(self._someList)
     ...

Alan



More information about the Python-list mailing list