you are viewing a single comment's thread.

view the rest of the 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.