Python Language Common Pitfalls Pythonic JSON keys

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Example

my_var = 'bla';
api_key = 'key';
...lots of code here...
params = {"language": "en", my_var: api_key}

If you are used to JavaScript, variable evaluation in Python dictionaries won't be what you expect it to be. This statement in JavaScript would result in the params object as follows:

{
    "language": "en",
    "my_var": "key"
}

In Python, however, it would result in the following dictionary:

{
    "language": "en",
    "bla": "key"
}

my_var is evaluated and its value is used as the key.



Got any Python Language Question?