[ZDP] BackTalk to Document Zope Developer's Guide (2.4 edition)/ZODB Persistent Components
webmaster@zope.org
webmaster@zope.org
Sat, 05 Oct 2002 10:55:41 -0400
A comment to the paragraph below was recently added via http://www.zope.org/Documentation/Books/ZDG/current/Persistence.stx#3-61
---------------
One tricky type of non-persistent, shared objects are mutable
default arguments to functions, and methods. Default arguments
are useful because they are cached for speed, and do not need to
be recreated every time the method is called. But if these cached
default arguments are mutable, one thread may change (mutate) the
object when another thread is using it, and that can be bad. So,
code like::
def foo(bar=[]):
bar.append('something')
% Anonymous User - Sep. 6, 2002 9:49 am:
need more explanation, please: what if foo is a method of class FooClass(Persistence.Persistent)? Does the
persistence machinery guarantee threadsafety for argument 'bar' or we are in trouble all the same?
Put another way, arguments of methods in persistent classes are persistent or not?
% beyond - Oct. 5, 2002 10:55 am:
(slightly off-topic)
def foo(bar=[]) looks dangerous to me.
When you use the default argument bar it will always be the same mutable
sequence. So calling
def myFoo(bar=[]):
bar.append('bla')
print bar
two times will result in ['bla'] and ['bla','bla']