Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Equivalent of TCL's "subst" ? (http://www.velocityreviews.com/forums/t551744-equivalent-of-tcls-subst.html)

gamename 11-13-2007 11:00 PM

Equivalent of TCL's "subst" ?
 
Hi,

In TCL, you can do things like:
set foobar "HI!"
set x foo
set y bar
subst $$x$y
HI!

Is there a way to do this type of evaluation in python?

TIA,
-T


Steven Bethard 11-13-2007 11:08 PM

Re: Equivalent of TCL's "subst" ?
 
gamename wrote:
> In TCL, you can do things like:
> set foobar "HI!"
> set x foo
> set y bar
> subst $$x$y
> HI!
>
> Is there a way to do this type of evaluation in python?


If this is at the outer-most scope, you can use globals()::

>>> foobar = 'HI!'
>>> x = 'foo'
>>> y = 'bar'
>>> globals_dict = globals()
>>> globals_dict[x + y]

'HI!'

That said, why do you think you want to do this?

STeVe

Larry Bates 11-14-2007 12:49 AM

Re: Equivalent of TCL's "subst" ?
 
gamename wrote:
> Hi,
>
> In TCL, you can do things like:
> set foobar "HI!"
> set x foo
> set y bar
> subst $$x$y
> HI!
>
> Is there a way to do this type of evaluation in python?
>
> TIA,
> -T
>


myStore={}

myStore['foobar']="HI!"
x='foo'
y='bar'
print myStore[x+y]

-Larry

Loic Mahe 11-14-2007 10:15 AM

Re: Equivalent of TCL's "subst" ?
 
gamename a écrit :
> Hi,
>
> In TCL, you can do things like:
> set foobar "HI!"
> set x foo
> set y bar
> subst $$x$y
> HI!
>
> Is there a way to do this type of evaluation in python?
>
> TIA,
> -T
>


you can also try using eval:

>>> foobar = "HI!"
>>> x = 'foo'
>>> y = 'bar'
>>> print eval(x+y)

HI!



Loic

gamename 11-17-2007 07:11 PM

Re: Equivalent of TCL's "subst" ?
 
> >>> foobar = "HI!"
> >>> x = 'foo'
> >>> y = 'bar'
> >>> print eval(x+y)

> HI!


Great! Thanks.
-T


All times are GMT. The time now is 11:53 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.