Python – PEP8 Style Guide for Python Code

I came across this page, PEP is short for Python Enhancement Proposal, and PEP8 is the proposal which could/should be used as the guidelines for writing Python code.

The article is not too long but definitely not easy to digest in a short time either. Here are a few take-aways that I did not know before hand.

1. leading underscore (_variable) is a indicator that it is “weak internal use”. Also, when you import * from a package, objects start with one leading underscore will not be imported.

2. double leading underscore means name mangling which will concatenate the class name with the method for internal use (class myclass: … __mymethod will be myclass__mymethod__). For example, when you write Python following OOP style, you can declare an attribute with double leading underscore to make that attribute not visible to the outside.
Check out this example.

3. For the variables which have double leading and trailing underscores, those “magic” objects or attributes live in user-controller namespace. Don’t invent them and only use them as documented.

4. when implementing ordering operations with rich comparisons, it is recommended to implement all 6 operations like (__eq__:==, __ne__:!=, __lt__:<, __le__:<=, __gt__:>, __ge__:>=)


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s