Equivalent of TCL's "subst" ?

Discussion in 'Python' started by gamename, Nov 13, 2007.

  1. gamename

    gamename Guest

    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
    gamename, Nov 13, 2007
    #1
    1. Advertising

  2. 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
    Steven Bethard, Nov 13, 2007
    #2
    1. Advertising

  3. gamename

    Larry Bates Guest

    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
    Larry Bates, Nov 14, 2007
    #3
  4. gamename

    Loic Mahe Guest

    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
    Loic Mahe, Nov 14, 2007
    #4
  5. gamename

    gamename Guest

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

    > HI!


    Great! Thanks.
    -T
    gamename, Nov 17, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Vinay Gupta
    Replies:
    2
    Views:
    1,104
    Michael Schlenker
    Jul 27, 2004
  2. subst and IIS on XP

    , Jan 23, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    1,453
  3. Chris
    Replies:
    1
    Views:
    598
    Chad Z. Hower aka Kudzu
    Feb 17, 2004
  4. Marv

    Quick Regex Subst question

    Marv, Dec 28, 2003, in forum: Perl Misc
    Replies:
    2
    Views:
    251
    Eric Amick
    Dec 28, 2003
  5. Mumia W.

    Inline::Tcl vs. Inline::Tcl

    Mumia W., Aug 23, 2006, in forum: Perl Misc
    Replies:
    0
    Views:
    137
    Mumia W.
    Aug 23, 2006
Loading...

Share This Page