* Class Parameters: important change of behavior for more safety to avoid

potential confusion.

* Detail of the important changes:

  1) by default now the function's local variables are not included in the
     lookup chain. Use _localvars_ flag if you insist on doing that; but
     be aware of the caveat (see Parameters' documentation).

  2) the default _kwparam_ and _userparam_ names are now passed on to the
     child Parameters object instead of the override values given through
     the _create_ function argument.
     Rationale: To begin with, the default names defined during the creation
     of the parent Parameters object should be used by *all* the relevant
     functions.
     Overrides should be regarded as exceptional rather than common cases.

  3) Value overrides (_kwparam_, _userparam_, etc.) in _create_ must now
     be specified in keyword=value manner rather than as positional argument.
     Rationale: Those overrides must be made conspicuous.
     The keyword=value approach also makes the code more resilient to minor
     API changes.
     All unnamed parameters are supposed to be low-priority defaults.
This commit is contained in:
wirawan
2011-09-09 18:58:48 +00:00
parent 4aff740f55
commit aad559d86b
2 changed files with 99 additions and 25 deletions

View File

@@ -1,8 +1,14 @@
# $Id: params_flat_test.py,v 1.1 2010-09-30 16:16:38 wirawan Exp $
# $Id: params_flat_test.py,v 1.2 2011-09-09 18:58:48 wirawan Exp $
# 20100930
from wpylib.params import flat as params
global_defaults = params(
nbasis= 275,
npart= 29,
deltau= 0.01,
)
def test1():
defaults = {
'nbasis': 320,
@@ -10,7 +16,9 @@ def test1():
'deltau': 0.025,
}
p = params(defaults, nbasis=332)
nbasis = 327
print "test1()"
print "self-defined values = ", p
print "nbasis = ", p.nbasis
print "npart = ", p.npart
@@ -19,6 +27,37 @@ def test1():
print "new deltau = ", p.deltau
def test2(**_opts_):
p = global_defaults._create_(_localvars_=1)
nbasis = 327
print "test2()"
print "self-defined values = ", p
print "nbasis = ", p.nbasis # gives 275 -- although _localvars_ already requested.
print "npart = ", p.npart
print "deltau = ", p.deltau
p.deltau = 0.01
print "new deltau = ", p.deltau
def test2b(**_opts_):
nbasis = 327
p = global_defaults._create_(_localvars_=1)
nbasis = 3270
print "test2()"
print "self-defined values = ", p
print "nbasis = ", p.nbasis # gives 327. Changes to local vars won't alter anything.
print "npart = ", p.npart
print "deltau = ", p.deltau
p.deltau = 0.01
print "new deltau = ", p.deltau
if __name__ == "__main__":
test1()
test2()
test2b()