Python coding style
NickName:andrean Ask DateTime:2012-06-08T00:42:59

Python coding style

I've been reading various python coding style guides, some answers on SO, etc. but none of them mentions some maybe not that important questions, but I would like to know if there is a preferred way for doing this:

In case I have a dictionary, which style would be better to use:

dict_name = {'test': 'somevalue',
             'test2': 'other'}

or

dict_name = {
    'longer_key': 'somevalue',
    'longer_key2': 'other'
}

or

dict_name = {
             'test': 'somevalue',
             'test2': 'other'
}

or

dict_name = {
             'test': 'somevalue',
             'test2': 'other'
            }

or something else?

Also for when calling methods:

function_name(longer_arg1, longer_arg2, longer_arg3, 
    longer_arg4)

or

function_name(longer_arg1, longer_arg2, longer_arg3, 
              longer_arg4)

or

function_name(
    longer_arg1, 
    longer_arg2, 
    longer_arg3, 
    longer_arg4
)

or

function_name(
              longer_arg1, 
              longer_arg2, 
              longer_arg3, 
              longer_arg4
)

or something else?

when a longer logging line is used, let's say:

loggername.info('this is an awfully long line which must be separated'
    'into two lines, am I doing it right? {0}'.format('nope..'))

or even consider this:

loggername.info('this is an {0} {1} line which must be separated'
    'into {2} lines, am I doing it right? {0}'.format(
    'awfully', 'short', 'three', 'nope..')
)

Now this last is somewhat related to the function calling style too, we have many arguments, a long string, how would be the best to separate these kind of lines?

Copyright Notice:Content Author:「andrean」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/10936164/python-coding-style

Answers
Levon 2012-06-07T16:43:50

You can't go wrong with looking at the PEP 8 - The Style Guide for Python Code for guidance on how to write readable Python code. Highly recommended.",


More about “Python coding style” related questions

Python and Django coding style (PEP)

I write code with Python using Django framework. Now I have read about all these coding style advices, but encountered a vague thing. In djangoproject section here https://docs.djangoproject.com/e...

Show Detail

Coding Style in Python

I have been interviewed recently by an eminent silicon valley based firm . I had been asked to write the code for the following question with O(n) time complexity . Question : Write a function tha...

Show Detail

Change default python coding style

In python i'm following camelCase Naming style. I checked my code with "pylint" and it gives error for not following lower_case_with_underscores style. Also i use netBeans IDE for coding. This IDE ...

Show Detail

Is there a way to enforce variable declarations in Python through coding style?

I'm using PyCharms to work on Python projects. I find that writing functions is easier when we declare the type of the function parameters or the type of the new variables declared explicitly. It a...

Show Detail

Common coding style for Python?

I'm pretty new to Python, and I want to develop my first serious open source project. I want to ask what is the common coding style for python projects. I'll put also what I'm doing right now. 1.-...

Show Detail

Python Coding style Wrapping Lines

As described on http://docs.python.org/tutorial/controlflow.html : Wrap lines so that they don’t exceed 79 characters. Usualy i use breaklines on line 80, but sometimes i have functions that re...

Show Detail

Anyone have a favorite Python coding style enforcer?

I'm trying to find a Python coding style enforcer (tool, not person!). Any recommendations?

Show Detail

Python coding style

I've been reading various python coding style guides, some answers on SO, etc. but none of them mentions some maybe not that important questions, but I would like to know if there is a preferred wa...

Show Detail

Global variables and coding style recommendations

This question is quite general but it's more for me to understand good coding practices in python... I'd like to define a constant that I can use inside in any function without having to pass it as

Show Detail

Is coding style mostly for readability or are there other factors?

I remember reading in Douglas Crockford's "Javascript the Good Parts" book that there is potential for error with blockless statements because of automatic semicolon insertion. if (condition) f...

Show Detail