Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct way to put long function calls on multiple lines

I have a long function, as seen below:

hash_correct = hashlib.md5(salt + password)).digest().encode("base64")

I'd like to split it up into two lines but am not sure of the correct way to do this in Python?

Thanks.

like image 222
ensnare Avatar asked Mar 31 '10 04:03

ensnare


1 Answers

The coding guidelines limiting length of lines is there, in part, to make the code more readable. In your case of chained method calls, the meaning is not clear. You should pick some temporary variable names for the intermediate values so that a reader of the code can understand the chain easily.

One example might be:

safe_md5 = hashlib.md5(salt + password)
crypto_hash = safe_md5.digest()
hash_correct = crypto_hash.encode('base64')

This leads the reader down a garden path to understanding. Very little is lost in performance, and the additional code is all added for purpose.

like image 195
Charles Merriam Avatar answered Oct 23 '22 14:10

Charles Merriam