[Tutor] Perl Symbology

Bill Mill bill.mill at gmail.com
Thu Feb 10 19:59:58 CET 2005


Kent,

On Thu, 10 Feb 2005 13:43:21 -0500, Kent Johnson <kent37 at tds.net> wrote:
> Python 2.4 includes a string.Template class which does much the same thing as Itpl.itpl():
> 
>   >>> from string import Template
>   >>> s, n, r = '0', 12, 3.4
>   >>> x = Template("$s $n $r")
>   >>> x.substitute(locals())
> '0 12 3.4'
> 
> If you want to bundle this up in a pp() function you have to do some magic to get the locals() of
> the caller. This seems to work:
> 
>   >>> import sys
>   >>> def pp(s):
>   ...   loc = sys._getframe(1).f_locals
>   ...   print Template(s).substitute(loc)
>   ...
>   >>> pp("$s $n $r")
> 0 12 3.4
> 

I just didn't want to give an answer that only works in python 2.4,
and one furthermore which I have not tested.

You can also find a recipe to make pp() easy to make at 
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/335308 . This
function may or may not be better than yours; I haven't used either.
the Itpl module has worked fine for me (and for the author of ipython)
for quite a while.

Peace
Bill Mill
bill.mill at gmail.com


More information about the Tutor mailing list