Jump to content

Python commenting style

namarino

Hey guys, I'm just learning Python. What is considered good style when commenting? What should you comment? For example, I'm writing a very simple program for class right now that is just a series of if else. There are no functions or anything. I was thinking that I would just have to comment the if statements. But then I thought that they're already pretty self explanatory. Anyway, thanks for your help!

Link to comment
Share on other sites

Link to post
Share on other sites

If it's obvious what something does, you don't need to comment. However if you need to take a closer look to see what's going on, comment it.

GPU: Gigabyte GTX 970 G1 Gaming CPU: i5-4570 RAM: 2x4gb Crucial Ballistix Sport 1600Mhz Motherboard: ASRock Z87 Extreme3 PSU: EVGA GS 650 CPU cooler: Be quiet! Shadow Rock 2 Case: Define R5 Storage: Crucial MX100 512GB
Link to comment
Share on other sites

Link to post
Share on other sites

they're comments...

write whatever you want

it doesnt matter unless youre planning to post your code online for others to use, in which case you should be explaining stuff more complex than an if statement.

NEW PC build: Blank Heaven   minimalist white and black PC     Old S340 build log "White Heaven"        The "LIGHTCANON" flashlight build log        Project AntiRoll (prototype)        Custom speaker project

Spoiler

Ryzen 3950X | AMD Vega Frontier Edition | ASUS X570 Pro WS | Corsair Vengeance LPX 64GB | NZXT H500 | Seasonic Prime Fanless TX-700 | Custom loop | Coolermaster SK630 White | Logitech MX Master 2S | Samsung 980 Pro 1TB + 970 Pro 512GB | Samsung 58" 4k TV | Scarlett 2i4 | 2x AT2020

 

Link to comment
Share on other sites

Link to post
Share on other sites

they're comments...

write whatever you want

it doesnt matter unless youre planning to post your code online for others to use, in which case you should be explaining stuff more complex than an if statement.

....yes I know....I was asking about style not content...

Link to comment
Share on other sites

Link to post
Share on other sites

....yes I know....I was asking about style not content...

oh really?

then why did you type

 

What should you comment?

NEW PC build: Blank Heaven   minimalist white and black PC     Old S340 build log "White Heaven"        The "LIGHTCANON" flashlight build log        Project AntiRoll (prototype)        Custom speaker project

Spoiler

Ryzen 3950X | AMD Vega Frontier Edition | ASUS X570 Pro WS | Corsair Vengeance LPX 64GB | NZXT H500 | Seasonic Prime Fanless TX-700 | Custom loop | Coolermaster SK630 White | Logitech MX Master 2S | Samsung 980 Pro 1TB + 970 Pro 512GB | Samsung 58" 4k TV | Scarlett 2i4 | 2x AT2020

 

Link to comment
Share on other sites

Link to post
Share on other sites

oh really?

then why did you type

Yes I said what kind of things should I comment not what to write in the comments.

Link to comment
Share on other sites

Link to post
Share on other sites

Yes I said what kind of things should I comment not what to write in the comments.

Yes and I said stuff more complex than an if statement, I already answered that question

 

The point of comments is to explain a piece of code that someone wouldn't comprehend, such as

Dvector Solvex(const Dmatrix &Amat, const Dvector &bvect){    Dvector sol;    float answer2=Det(Amat);    for (int x=0; x<bvect.size(); ++x)    {        float answer1=Det(Replace(Amat,x,bvect));        sol.push_back(answer1/answer2);    }    return(sol);}

then someone who knows the programming language might not know what this piece of code is doing, so you explain that in a comment like "dividing the determinant of the a matrix by the original a matrix, we can find the solutions of x"

 

comments are not to teach people how the language works

they are for comprehensive reasons

NEW PC build: Blank Heaven   minimalist white and black PC     Old S340 build log "White Heaven"        The "LIGHTCANON" flashlight build log        Project AntiRoll (prototype)        Custom speaker project

Spoiler

Ryzen 3950X | AMD Vega Frontier Edition | ASUS X570 Pro WS | Corsair Vengeance LPX 64GB | NZXT H500 | Seasonic Prime Fanless TX-700 | Custom loop | Coolermaster SK630 White | Logitech MX Master 2S | Samsung 980 Pro 1TB + 970 Pro 512GB | Samsung 58" 4k TV | Scarlett 2i4 | 2x AT2020

 

Link to comment
Share on other sites

Link to post
Share on other sites

Check out the popular PEP 8 style guide for Python. It has a section on comments as well as a lot of other stuff.

 

Yes. PEP8 is what you want.

--Neil Hanlon

Operations Engineer

Link to comment
Share on other sites

Link to post
Share on other sites

There's also a school of thought that dictates that there should never usually be a need for comments at all. All code should be written such that it is completely self describing.

While I ascribe to this principle (I think it also makes one a better developer), in reality there are caveats.

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×