Duncan Booth wote: > Also, of course, vars just needs to be something which quacks like a dict: > it can do whatever it needs to do such as looking up a database or querying > a server to generate the value only when it needs it, or even evaluating > the name as an expression; in the OP's case it could call get_variable.
And in case that sounds difficult, the code is class VariableGetter: def __getitem__(self, key): return get_variable(key) > Anyway, the question seems to be moot since the OP's definition of 'elegant > and lazy' includes regular expressions and reinvented wheels. Your suggestion of subclassing string.Template will also require a regular expression -- and a fairly hairy one as far as I can work out from the documentation. There isn't an example and I don't think it's the easiest way of solving this problem. But if Samuel really wants backslash escaping it'd be easier to do a replace('$$','$$$$') and replace('\\$', '$$') (or replace('\\$','\\$$') if he really wants the backslash to persist) before using the template. Then, if he really does want to reject single letter variable names, or names beginning with a backslash, he'll still need to subclass Template and supply a regular expression, but a simpler one. > ... and in another message Graham Breed wrote: > > def get_variable(varname): > > return globals()[varname] > > Doesn't the mere thought of creating global variables with unknown names > make you shudder? Not at all. It works, it's what the shell does, and it's easy to test interactively. Obviously the application code wouldn't look like that. Graham -- http://mail.python.org/mailman/listinfo/python-list