Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python function argument list formatting

What is the best way to format following piece of code accordingly to PEP8:

oauth_request = oauth.OAuthRequest.from_consumer_and_token(consumer,
    token=token, verifier=verifier, http_url=ACCESS_TOKEN_URL)

The problem is that if I place more than one parameter on the first line, the line exceeds 79 characters. If I place each of the parameters on a separate line with 4 spaces indentation it looks quite ugly:

oauth_request = oauth.OAuthRequest.from_consumer_and_token(
    consumer,
    token=token,
    verifier=verifier,
    http_url=ACCESS_TOKEN_URL)

The best option that I come up with is to add extra indentation for better distinguishing:

oauth_request = oauth.OAuthRequest.from_consumer_and_token(
                        consumer,
                        token=token,
                        verifier=verifier,
                        http_url=ACCESS_TOKEN_URL)

I try to work out a general rule for me to use it for methods with long invocation on the first line and several parameters that can't fit a single line.

like image 909
sam Avatar asked Jul 07 '11 11:07

sam


People also ask

Can a function argument be a list?

You can send any data types of argument to a function (string, number, list, dictionary etc.), and it will be treated as the same data type inside the function.

What are the 4 types of format arguments in calling a function in Python?

5 Types of Arguments in Python Function Definition:positional arguments. arbitrary positional arguments. arbitrary keyword arguments.

How do you pass list elements as parameters in Python?

In Python, you can unpack list , tuple , dict (dictionary) and pass its elements to function as arguments by adding * to list or tuple and ** to dictionary when calling function.

What is list parameters in Python?

list() Parameters. The list() constructor takes a single argument: iterable (optional) - an object that could be a sequence (string, tuples) or collection (set, dictionary) or any iterator object.


1 Answers

My reading of the documentation suggests that 2 and 3 are both acceptable, but it looks like 2 is preferred (I say this because it looks like 2 vs. 3 is handled this way in the examples, I don't think that the style specification is very specific here). 1 is out (look at the docs under the line Arguments on first line forbidden when not using vertical alignment)

like image 78
cwallenpoole Avatar answered Oct 08 '22 22:10

cwallenpoole