To 'with' or not to 'with': how is the question ?

Niki Spahiev spahievi at
Sat Sep 2 21:51:02 CEST 2000

Saturday, September 02, 2000, 12:26:17 AM, Jason Cunliffe wrote:

JC> The with action is useful for accessing multiple items in a scope chain list
JC> simultaneously. In the following example, the built-in Math object is placed
JC> at the front of the scope chain. Setting Math as a default object resolves
JC> the identifiers cos, sin, and PI to Math.cos, Math.sin, and Math.PI,
JC> respectively. The identifiers a, x, y, and r are not methods or properties
JC> of the Math object, but since they exist in the object activation scope of
JC> the function polar, they resolve to the corresponding local variables.

JC> function polar(r){
JC>  var a, x, y
JC>  with (Math) {
JC>   a = PI * r * r
JC>   x = r * cos(PI)
JC>   y = r * sin(PI/2)
JC> }
JC> trace("area = " +a)
JC> trace("x = " + x)
JC> trace("y = " + y)
JC> }
JC> You can use nested with actions to access information in multiple scopes. In
JC> the following example, the instance fresno and the instance salinas are
JC> children of the instance california. The statement sets the _alpha values of
JC> fresno and salinas without changing the _alpha value of california.

JC> with (california){
JC>  with (fresno){
JC>   _alpha = 20;
JC>  }
JC>  with (salinas){
JC>   _alpha = 40;
JC>  }
JC> }

AFAIK ZOPE does that with it's MultiMap extension class. So if we use
MultiMaps intead of dicts as namespace, we will get such behaviour.

Best regards,
 Niki Spahiev

More information about the Python-list mailing list