-6

In python, I understand it's possible to convert many types of data to a string using str(). Is there any way to reverse this? Let me show you what I mean.

exampleDictionary = {'thing on ground': 'backpack'}
backpack = {'tea': 'Earl Grey'}

def openBackpack():
     #code to grab backpack from exampleDictionary
     #code to convert 'backpack' to backpack
     #code to access backpack and look at the tea

This is oversimplified of my code in progress, but it should be easy to see basically where I'm stuck at. If it's not clear I'm happy to clarify further.

  • 1
    Could you provide an example of a string you want to convert and what it should convert to? – Scott Hunter Jan 21 '15 at 19:15
  • Where is there conversion of any string to any dictionary in this question at all? – Charles Duffy Jan 21 '15 at 19:20
  • I want to convert the 'backpack' in example dictionary to a nonstring, so that my code recognizes 'backpack' as a dictionary and not a string. Right now it's giving me a "TypeError: string indices must be integers". – Adam Agnello Jan 21 '15 at 19:22
  • @AdamAgnello: `backpack` is a dictionary. Your code must be doing something else wrong, why not show the code that throws the exception? – Martijn Pieters Jan 21 '15 at 19:25
  • @AdamAgnello: are you trying to turn the value of `exampleDictionary['thing on ground']`, which is a string, to resolve the variable? Don't do that, create another dictionary (`objects = {'backpack': {...}}` for example) and look items up in that. – Martijn Pieters Jan 21 '15 at 19:26
  • Also see [How do I do variable variables in Python?](http://stackoverflow.com/q/1373164) – Martijn Pieters Jan 21 '15 at 19:27

3 Answers3

3

This is a very weird way to handle data.

I would use a nested dicts:

exampleDictionary = {'thing on ground': {'backpack': {'tea': 'Earl Grey'}}}

print exampleDictionary['thing on ground']
print exampleDictionary['thing on ground']['backpack']
print exampleDictionary['thing on ground']['backpack']['tea']

Outputs:

{'backpack': {'tea': 'Earl Grey'}}
{'tea': 'Earl Grey'}
Earl Grey
f.rodrigues
  • 3,499
  • 6
  • 26
  • 62
0

You're looking for globals().

def openBackpack():
    backpack= globals()[exampleDictionary['thing on ground']]
    print(backpack['tea'])
Aran-Fey
  • 39,665
  • 11
  • 104
  • 149
0

This approach looks up the string for and already existing object in the code.

exampleDictionary = {'thing on ground': 'backpack'}
backpack = {'tea': 'Earl Grey'}
print( eval(exampleDictionary['thing on ground']) ) 

edit

to Adam Smith 's point eval isn't a safe and you shouldn't do this

... but it can be done and here is how.

eval https://docs.python.org/2/library/functions.html#eval

takes a string and interprets it as code.

x = eval("1+2") 
x = 3 

if you eval user input the likelyhood you know all of what a user can input or 'inject' into your code is unknown.

corn3lius
  • 4,857
  • 2
  • 31
  • 36
  • Unless of course any of that is user input, in which case `exampleDictionary` may look like `{'thing on ground': "os.system.do_something_terribly_irreversible_to_your_system('now')"}`. `eval` isn't safe. – Adam Smith Jan 21 '15 at 19:30
  • @AdamSmith I realize its not good form but his question was he wants a string to reference an object. – corn3lius Jan 21 '15 at 19:31
  • 1
    Right, but the correct thing to do when a novice asks how to build a house of Styrofoam is to *strongly recommend that he use wood instead*, not teach him how to glue it together – Adam Smith Jan 21 '15 at 19:32
  • @AdamSmith Why should I not do the above example? – Adam Agnello Jan 21 '15 at 19:35
  • @AdamAgnello `eval` turns any string into a Python command. Many strings are UNSAFE PYTHON COMMANDS, such as `"[os.remove(file) for file in glob.glob('C:\windows\system32\*')]"` (***do not run that***). Most questions of this type are based on using user input to look for a variable name, which means you're allowing a user to run arbitrary code in your program. That's a Bad Thing. – Adam Smith Jan 21 '15 at 19:38
  • 1
    load the gun @AdamSmith ... he could blow his leg off. – corn3lius Jan 21 '15 at 19:40
  • @corn3lius in my defense, my first example was not only a placeholder method, but *inside a module that doesn't exist*. Since he asked, though.... – Adam Smith Jan 21 '15 at 19:46
  • @AdamSmith would you mind if I sent you my project that I'm working on? I've never actually been critiqued before an I could use a learning experience. Because you know, I'd rather not blow my leg off as corn3lius suggests might happen. – Adam Agnello Jan 21 '15 at 19:47
  • @AdamAgnello I don't mind looking at it when I have time, but I'm not big on sharing my personal contact info. Toss it in a `codepad.org` doc (or pick your favorite textdump) and link it – Adam Smith Jan 21 '15 at 19:48
  • 1
    or better yet http://codereview.stackexchange.com/ – corn3lius Jan 21 '15 at 19:49
  • http://codepad.org/HkQBzjhU Here ya go. This is my first project past 50 lines of code, so I'm sure it's a nightmare to you more experienced people. – Adam Agnello Jan 21 '15 at 19:50
  • @corn3lius Thanks. I'll post it there too. – Adam Agnello Jan 21 '15 at 19:51
  • Here's a link to the codereview for all interested. http://codereview.stackexchange.com/questions/78255/newbies-first-project – Adam Agnello Jan 21 '15 at 20:02
  • As mentioned above, there is absolutely nothing wrong with using eval, if you aren't evaluating user input, beyond the fact there are much better alternatives to achieving dynamic code (assuming this is the intent here). – Eithos Jan 21 '15 at 20:38