Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Python中限制复杂行的长度?_Python_Pep8_Continuation - Fatal编程技术网

如何在Python中限制复杂行的长度?

如何在Python中限制复杂行的长度?,python,pep8,continuation,Python,Pep8,Continuation,我正在根据格式化我的代码,但我有一个小问题;就是这样: print ("DB Updated: " + datetime.datetime.fromtimestamp(int(stats_dict["db_update"])).strftime('%a %b %d %H:%M:%S %Y')) 如何将它分成72-79个字符的行?我更喜欢: print ("DB Updated: " + datetime.datetime.fromtimestamp(\ int(stats_dict[

我正在根据格式化我的代码,但我有一个小问题;就是这样:

print ("DB Updated: " + datetime.datetime.fromtimestamp(int(stats_dict["db_update"])).strftime('%a %b %d %H:%M:%S %Y'))
如何将它分成72-79个字符的行?

我更喜欢:

print ("DB Updated: " + datetime.datetime.fromtimestamp(\
     int(stats_dict["db_update"]))\
     .strftime('%a %b %d %H:%M:%S %Y'))
print("DB Updated: " + 
    datetime.datetime.fromtimestamp(
        int(stats_dict["db_update"])
    ).strftime('%a %b %d %H:%M:%S %Y')
)
请注意,只要整个表达式在大括号中,就不需要行连续符号

在我看来,临时变量会降低可读性

OP更新:我修改了你的答案如下:

 print("DB Updated: " +
      datetime.datetime.fromtimestamp(
        int(stats_dict["db_update"])).
        strftime('%a %b %d %H:%M:%S %Y'))

第二行太长(80个字符)。考虑一下可能的情况。这是一团糟。要理解它,你需要跳到第三行,然后是第二行,然后是第四行,然后是第一行。与之相比
print("DB Updated: " + 
    datetime.datetime.fromtimestamp(
        int(stats_dict["db_update"])
    ).strftime('%a %b %d %H:%M:%S %Y')
)
 print("DB Updated: " +
      datetime.datetime.fromtimestamp(
        int(stats_dict["db_update"])).
        strftime('%a %b %d %H:%M:%S %Y'))
from datetime import datetime

dt = datetime.fromtimestamp(int(stats_dict["db_update"]))
print("DB Updated: {:%a %b %d %H:%M:%S %Y}".format(dt))