2

VSCode has syntax highlighting for the Python language. I can't find any explanation for how each token is classified and organized into colors.

Specifically, why are some keywords the same color, but some are different?

Looking at this code:

Code example in VSCode

for...in shows the keywords as the same color, but if...in shows the keywords as different colors. Why does in change color? What determines if a keyword is purple or blue or something else?

Finally, is there an exhaustive list or chart for how keywords are organized?

3
  • for...in and if...in are python keywords Commented Jul 12, 2020 at 21:55
  • @Trevin Avery Have you got the answer you need? Commented Jul 22, 2020 at 12:20
  • @JillCheng Yes, I just found it and updated the question with the exact answer. Thanks. Commented Jul 22, 2020 at 16:48

3 Answers 3

2

The color of the code in VSCode is provided by the theme. (VSCode uses its own theme by default.) Reference: syntax-highlighting-optimizations and theme color.

Using different themes can make the code display different colors. You could also set the color of custom code. Reference: Customizing a Color Theme.

Sign up to request clarification or add additional context in comments.

Comments

1

They are not the same in. The first is used to iterate while the second is an operator.

Comments

0

From the first link in @JillCheng's answer I found the Scope Inspector, which can be enabled by running Developer: Inspect Editor Tokens and Scopes in the command palette. This will then show how the currently selected text was tokenized.

for...in shows both keywords are tokenized as keyword.control.flow.python.

if...in shows if as keyword.control.flow.python and in as keyword.operator.logical.python.


Note: This answer was originally posted as part of the question.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.