Persistent variable in Python Script? Is this a feature or a bug?
Given a Python Script A which only returns an array ======== return ('a') ======== Then call A in another Python Script B with parameter list: b=[] ======== b.append(container.A()) return b ======== Then request B in browser. Guess what happened? ['a'] correct! But what if you "reload" B again and again? You will get ['a','a','a',........] Well, why this happen? A feature or A bug? I thought that the parameter should be cleared at each new request. How to get a "local" parameter without this persistent feature? Any idea? Thanks a lot. Iap, Singuan
On Tue, 09 Apr 2002 16:03:50 +0800 iap@y2fun.com wrote:
Given a Python Script A which only returns an array ======== return ('a') ========
Then call A in another Python Script B with parameter list: b=[]
======== b.append(container.A()) return b ========
Then request B in browser. Guess what happened? ['a'] correct! But what if you "reload" B again and again? You will get ['a','a','a',........]
Well, why this happen? A feature or A bug?
Zope stores non-persistent objects until the next restart in the memory... so appending something to a list will give you more and more list-items...
I thought that the parameter should be cleared at each new request. How to get a "local" parameter without this persistent feature?
Don't use b=[] in the parameter-list... instead: b=[] b.append(container.A()) return b greetings, maik. -- maik jablonski http://www.sachunterricht-online.de universitaet bielefeld http://www.zfl.uni-bielefeld.de zentrum fuer lehrerbildung tlph://+49.(0).521.106.4234
Thanks. Actually, I use A to provide some common options, and call B from some other C with varied individual options. I also want A to be included not only by B but also by B1, B2..... That's why I have to set parameter list. (A,B,C,...just for abstraction) Current, I have a work-around: Another version of B works: parameter list: b=[] ====== bb=[] if b: bb.extend(b) bb.extend(container.A()) return bb ====== this is interesting, that "if b" got false value in this case. Iap, Singuan
b=[] b.append(container.A()) return b
greetings, maik.
-- maik jablonski http://www.sachunterricht-online.de universitaet bielefeld http://www.zfl.uni-bielefeld.de zentrum fuer lehrerbildung tlph://+49.(0).521.106.4234
iap@y2fun.com wrote:
Given a Python Script A which only returns an array ======== return ('a') ========
Then call A in another Python Script B with parameter list: b=[]
======== b.append(container.A()) return b ========
Then request B in browser. Guess what happened? ['a'] correct! But what if you "reload" B again and again? You will get ['a','a','a',........]
From the docs: 7.5 Function definitions Default parameter values are evaluated when the function definition is executed. This means that the expression is evaluated once, when the function is defined, and that that same ``pre-computed'' value is used for each call. This is especially important to understand when a default parameter is a mutable object, such as a list or a dictionary: if the function modifies the object (e.g. by appending an item to a list), the default value is in effect modified. This is generally not what was intended. A way around this is to use None as the default, and explicitly test for it in the body of the function, e.g.: def whats_on_the_telly(penguin=None): if penguin is None: penguin = [] penguin.append("property of the zoo") return penguin regards Max M
participants (3)
-
iap@y2fun.com -
Maik Jablonski -
Max M