正确的方法将长函数调用放在多行上 [英] Correct way to put long function calls on multiple lines

查看:54
本文介绍了正确的方法将长函数调用放在多行上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的功能很长,如下所示:

I have a long function, as seen below:

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

我想将其分为两行,但是不确定在Python中执行此操作的正确方法吗?

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

谢谢.

推荐答案

部分地限制行长度的编码准则可以使代码更具可读性.在链接方法调用的情况下,含义不明确.您应该为中间值选择一些临时变量名称,以使代码阅读者可以轻松理解链.

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.

一个例子可能是:

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.

这篇关于正确的方法将长函数调用放在多行上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆