#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    1
    Rep Power
    0

    String to Constant conversion


    HI,
    I am a new python programmer. Can anybody please tell me how can I convert a string to constant: for example i have a string
    "Key.Enter" I want to convert it to just Key.ENTER constant. Thts is

    "Key.ENTER" --> Key.ENTER

    Plz help me
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Location
    Iran
    Posts
    149
    Rep Power
    139
    Hi there,

    There's no equivalent for constants in Python, as the programmer is generally considered intelligent enough to leave a value he/she wants to stay constant alone.


    Regards,
    Dariyoosh
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    114
    Rep Power
    3
    I think what he means is, given a string containing a name, he wants the object with that name.

    Calling eval in the appropriate scope will do the trick, but chances are there's a less ugly solution given context.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2007
    Location
    Joensuu, Finland
    Posts
    434
    Rep Power
    67
    Originally Posted by Nyktos
    I think what he means is, given a string containing a name, he wants the object with that name.

    Calling eval in the appropriate scope will do the trick, but chances are there's a less ugly solution given context.
    If so, vars() is a safer solution:

    Code:
    >>> spam = 10
    >>> vars()['spam']
    10
    My armada: openSUSE 13.1 (home desktop, home laptop), Crunchbang Linux 11 (mini laptop, work laptop), Ubuntu 14.04 LTS (server), Android 4.2.1 (tablet), Windows 7 Ultimate (testbed)

IMN logo majestic logo threadwatch logo seochat tools logo