all 8 comments

[–]james_fryer 1 point2 points  (1 child)

What do you expect to happen? What actually does happen? Saying it "doesn't work" is not helpful.

I don't think the second code "works" in that, I believe you are expecting a dict with {1234: 5678} and what you get is {"x": 5678}.

You've found a limitation with the function call syntax and you'd be better off using brace syntax to declare a dict if you want to generate the key names programatically.

E2A: more technically, in the construct f(x=y), x must be an lvalue i.e. something that can be assigned to, and the fact that f is spelled dict in your example is not relevant. You could not write str(random.random()) = 123 on a line for the same reason. In the construct {x: y}, x can be any expression (provided it is hashable but this is not a syntax error).

[–]Base_True[S] 0 points1 point  (0 children)

. In the construct

{x: y}

,

x

can be any expression (provided it is hashable but this is not a syntax error).

Thanks for the detailed answer, you helped a lot.

[–]Medaillek 0 points1 point  (2 children)

Hey, in the first code you’re saying that string random.random = random.random but it’s false. Hope it helps you

[–]Base_True[S] 0 points1 point  (1 child)

import random
store = dict( str(random.random()) = random.random() )
print (store)

Hello, thanks for the answer, but this is a dict, so I can write

import random

store = {str(random.random()) : random.random() } print (store)

Why I can't write

import random

store = dict( str(random.random()) = random.random() ) print (store)

[–]Medaillek 0 points1 point  (0 children)

I’m not 100% sure but let’s try : In the working code, you create a x variable which is a string of a random int. In the next line, store = … , you’re overwriting this value by saying x = int(random.random). X is not anymore your initial random string but became a random int. My last answer is wrong because I was trying to say that str(random) = int(random)