+ 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) ```
5 Respuestas
+ 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
+ 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)
It is just unnecessary to have those particular comments in this particular code. That's what RuntimeError is pointing out. Most programmers will agree that using meaningful names for variables serve as sufficient documentation. If those variables must have certain values, there are programmatic ways to enforce that. But we won't cover that here. When you get into object oriented techniques, you'll learn about getters and setters that can enforce things like positive values only or valid currency types.
Isn't this just as easy, or easier to read?
currency = "USD"
amount = 200
status = "overdue"
print(status, amount, currency)
Compare that to this:
# Set the currency
currency = "USD"
# Set the amount
amount = 200
# Set the status
status = "overdue"
# Print the values
print(status, amount, currency)
It just doesn't add any clarification. But if you want comments, that's ok and the blank lines don't matter.
+ 1
This might be more of a personal preference I think. Once you join a team of devs, be it for work, side job, or whatever, the team usually has setup a convention on how code is written, including how comments are written within code.
However, during learning phase how we write comments in code doesn't matter. At this phase we write comments in code mostly as reminder, what these lines (or block of lines) do, or supoosed to do, or even a Description of the program.
We write commenta such that when we view the code again someday, we know what program it is (from Description in comment), how we did it back then, and what each block of code was supposed to do.
Don't bother yourself with little things during learning phase, I know how confusing things can get during the phase, I went crazy nut about commenting my code. I still go LMAO every now and then reviewing my codes from those days, remembering how hard it was then, for me, with so little things I knew.
It does not matter, none of our practice code will go into a serious project anyways, nor will violate team's code convention.
Code on dude :)
0
Hi, thank you very much for your reply. Actually, the code is not mine, it was just an example that SoloLearn gave me in a lesson. Anyway, I will take it into consideration.
0
Hi, thank you very much for your reply. Actually, the code is not mine, it was just an example that SoloLearn gave me in a lesson. I just wanted to know if line breaks or spaces can affect the code in any way. Anyways, I will take it into consideration.