In most languages, any variable defined inside a function is considered local.
# a Python function
def toRomanNumerals(num):
digits = # ...
string = ''
while num > 0:
(value, letters) = # ...
string = # ....
return string
In a Mathematica Module
, however, we must specify a list of all variables to treat as local.
toRomanNumerals[i_Integer?Positive] :=
Module[{num = i, string = "", value, letters, digits},
digits = (* ... *);
While[num > 0, {value, letters} =
(* ... *);
string = (* ... *).];
string]
I sometimes find it tedious to manually type the list of all local variables. Do others feel the same way?
Is there a way to create a Module
which treats all symbols defined inside as local?
Answer
This sort of programming is not my strength and I don't know Python. Reading the comments, perhaps this won't quite be perfect, but it might be good enough. It seems good enough for many purposes, at least in the way I interpret the question. It localizes all Symbols
in the code that are in the "Global`"
context and don't have Ownvalues
or DownValues
. If you want to exclude those with UpValues
etc., then it should be easy how to modify it to do so.
ClearAll[localizeAll];
SetAttributes[localizeAll, HoldAll];
localizeAll[code_] :=
With[{vars = Join @@ Union @
Cases[Hold[code],
s_Symbol /; Context[s] == "Global`" &&
Length@OwnValues[s] == 0 && Length@DownValues[s] == 0 :> Hold[s],
Infinity,
Heads -> True]},
(* Module[{##}, code] & @@ vars *) (* original *)
vars /. Hold[v___] :> Module[{v}, code] (* Leonid Shifrin's suggestion *)
]
Example:
Clear[z];
z[a_] := a^2;
localizeAll[x = 2; y = x++; z[x] = y; q[r_] := s; z[2x]]
z[3]
z[x]
q[1]
(* 36 *)
(* 2 *)
(* x^2 *)
(* q[1] *)
Note that because z
was defined, z[3]
got redefined.
I think I might rather take a few seconds and type out the variables explicitly. It might save time in the long run.
Comments
Post a Comment