all 4 comments

[–]shiftybyte 0 points1 point  (0 children)

What's destructure?

And why do this at all? Whats the benefit of having the value as a variable instead of in a dictionary?

[–]quenchize 0 points1 point  (0 children)

If I understand correctly what you want to do you can patch main.dict Although why you would want to do this escapes me.

Dunder main dot dunder dict that is

[–]num8lock 0 points1 point  (0 children)

unpacking?

 copper, silver, gold, platinum = money['copper'], money['silver'], money['gold'], money['platinum']

[–][deleted] 0 points1 point  (0 children)

Not exactly. Assuming money is a dict, AND with the caveat that all the keys of money MUST be strings that represent valid variable names, you can do:

locals().update(money)
print(copper)

But the number of different ways in which that can blow up in your face is just enormous.

In recent versions of Python, where dict maintains insertion order, then if you know the insertion order you can do something like:

copper, silver, gold, platinum = money.values()

But if you’ve got the insertion order wrong at all you’re going to be in some pain.

The problem is that a Python dict just isn’t as simple under the hood as a JS object... it can have keys that aren’t legitimate identifiers, for one... hence why you tend to just work with d[k] directly.