Skip to content
Advertisement

Why does this same expression return True in when entered as a line into the python interpreter and False when run in a script?

I have a python script where I’ve printed the value of two variables. These are dash callback id’s. An excerpt is:

JavaScript

The output is:

JavaScript

But when I copy and past the output of the first two lines and run them directly into the python3 interpreter I get:

JavaScript

I would expect these should both return the same boolean value. How is it these values are different? Furthermore, why am I getting False in the script and how can I change it so that it evaluates to True?

Advertisement

Answer

It looks like order_id is actually a string. If it were a dict, Python would use single-quotes there instead of double-quotes, and put spaces after the colons : and commas ,.

It seems to contain JSON, so use the json module to parse it.

JavaScript

In the future, you can use repr() or pprint.pprint() to diagnose issues like this.

JavaScript
JavaScript

(Note that pprint.pprint() sorts dict keys by default, which is less useful as of Python 3.7.)

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement