passing a custom locals to a function

Alex Martelli aleaxit at yahoo.com
Fri May 23 09:05:23 EDT 2003


Kuzminski, Stefan R wrote:
   ...
> am approaching it incorrectly, the main thing is that my users need to
> write formulas using values I have in a dictionary and they shouldn't
> need to know about the dictionary..

You can do it if what you have your users write are indeed "formulas",
but not if what you're having them write are *functions* instead.  A
function, as built by a def statement, has quite precise specifications
about what are "local functions" -- all those names that get bound in
the body, and those only -- and you can't change that without a LOT of
fighting against Python.  A "formula", i.e. an expression, now THAT is
something you can most easily 'transparently' feed from a dict.  I
suspect your needs may be somewhere in-between, namely "a bunch of
statements" -- not quite as pliant as a formula (which can't rebind
names except as the deuced side result of a list comprehension) but
not as rigid as a function either...


Alex





More information about the Python-list mailing list