[Zope-dev] Zope 2.3.1b1, strange things when ConflictError.
Steve Alexander
steve@cat-box.net
Mon, 26 Feb 2001 09:46:51 +0000
Erik Enge wrote:
> On Mon, 26 Feb 2001, Steve Alexander wrote:
>
>
>> Chris McDonough wrote:
>>
>>
>>> The use of a literal anonymous list in methodb's signature for "objects" may
>>> have something to do with the results you're getting on conflict. Try
>>> assigning "objects" to an empty list in the method body instead.
>>
>> Of course!
>
>
> I'm missing out on something here. Why is this an "of course"? What's a
> "literal anonymous list"?
Consider this method:
def methodFoo(self, objects=[]):
# do stuff, including altering the list objects
The list that is assigned to "objects" in the definition of arguments is
only created once. There is only one instance of the list for all the
calls to methodFoo. If you alter that list, then it is altered for the
future too.
Try it in Python from the interactive prompt:
>>> def foo(o=[]):
... o.append('x')
... print o
...
>>> foo()
['x']
>>> foo()
['x', 'x']
>>> foo()
['x', 'x', 'x']
>>>
--
Steve Alexander
Software Engineer
Cat-Box limited