Features Tips & Tricks Tutorials

Feature Spotlight: Reformatting Python code with Pycharm’s intentions

Happy Friday everyone!

Did you have a chance to read one of my previous posts on how PyCharm helps you write clean and maintainable Python code? As a quick recap: Pycharm highlights code style violations with both PEP8 and custom inspections, and it also allows you to apply automatic quick-fixes to keep your code in consistent format.
Today I’m going to cover another feature that you may find handy for writing professional and quality code. It’s called Code intentions.

The main difference between PyCharm’s code inspections and intentions is that while inspections provide quick-fixes for code that has potential problems, intentions help you apply automatic changes to code that is most likely correct.

Here’s how it works:

When editing absolutely correct code, the yellow bulb sometimes appears in the editor:


That signals that the automatic action is available to be applied in place. To get a list of intentions applicable to the code at the caret, just press Alt + Enter:


In this particular situation PyCharm offers to convert the lambda to a normal function. Just hit Enter and it will do the job for you:


It also highlights the new function name and when you change it, it automatically changes the function call:


There is a huge number of different code intentions available for Python and all other supported languages including JavaScript, SQL, CSS, and other. You can see them all as well as enable or disable some of them in Settings (Preferences for Mac OS) | Editor | Intentions:


Another way to get to the intentions settings or simply disable the unwanted intention is to hit right arrow on the intention in the editor:


I bet you’ll like this feature if you haven’t tried it before.

Have a great weekend and see you next week!


image description