+ 2

Is it OK to use a line break after a comment in Python?

For example: Do this: ``` #Declaring payment variables currency = "USD" amount = 200 status = "overdue" #Displaying status print(status) ``` Instead of this: ``` #Declaring payment variables currency = "USD" amount = 200 status = "overdue" #Displaying status print(status) ```

10th Nov 2024, 9:13 PM
CloudCode2
5 ответов
+ 2
It's not okay. But to be fair, both aren't okay especially that the comment is not needed for this particular instance. You don't have to tell anyone you are declaring payment variables or displaying something through comments. It's a bad commenting practice. comments should be made, only when necessary As regarding the line break, the second option follows pep-8, the first is alien. you can also do it this way currency = "...". # a comment for this only
10th Nov 2024, 9:35 PM
RuntimeTerror
RuntimeTerror - avatar
+ 2
The technical answer is yes. You can put blank lines wherever you want. Convention is to use comments when the code needs a little explanation. For simple things like assigning variables, you might comment that it can only be of a certain range so programmers will know what must be there to be valid. But no point to add comments that simply state what the code is obviously already doing. This code will work: #Declaring payment variables currency = "USD" amount = 200 status = "overdue" #Displaying status print(status)