线太长,如何在括号内划线

时间:2017-12-31 08:39:31

标签: python-3.x

在阅读PEP8并编辑我的代码后,我就想到了这一点:

 class Wheel(object):
    """A Virtual Wheel"""
    def __init__(self, start_number, stop_number, input_number, rotation_units, direction):
        self.start_number = start_number
        self.stop_number = stop_number
        self.input_number = input_number
        self.rotation_units = rotation_units
        self.direction = direction
        self.output_number = 0
        self.verify = None
        self.circumference_units_sum = 0

这是我在阅读PEP8后决定使用的:

class Wheel(object):
    """A Virtual Wheel"""
    def __init__(self, start_number, stop_number,
                 input_number, rotation_units, direction):
        self.start_number = start_number
        self.stop_number = stop_number
        self.input_number = input_number
        self.rotation_units = rotation_units
        self.direction = direction
        self.output_number = 0
        self.verify = None
        self.circumference_units_sum = 0

3 个答案:

答案 0 :(得分:1)

在Python中,只要您在括号内(使用括号,方形和卷曲),您就不需要任何特殊处理。只需写下:

class Wheel(object):
    """A Virtual Wheel"""
    def __init__(self, start_number, stop_number, input_number, rotation_units,
                 direction):
       self.start_number = start_number
       self.stop_number = stop_number
       self.input_number = input_number
       self.rotation_units = rotation_units
       self.direction = direction
       self.output_number = 0
       self.verify = None
       self.circumference_units_sum = 0

答案 1 :(得分:0)

您可以使用反斜杠字符(\)来分隔长行,但在括号之间您甚至不需要它(实际上,它是由pep8报告的!),以及可能只是打破了界限: E.g:

class Wheel(object):
    """A Virtual Wheel"""
    def __init__(self,
                 start_number,
                 stop_number,
                 input_number,
                 rotation_units,
                 direction):
        self.start_number = start_number
        self.stop_number = stop_number
        self.input_number = input_number
        self.rotation_units = rotation_units
        self.direction = direction
        self.output_number = 0
        self.verify = None
        self.circumference_units_sum = 0

答案 2 :(得分:0)

Python本身对你的行长度没有任何问题所以我必须假设你只是为了可读性而想要它。

如果是这种情况,我首先要看的是变量名称,你可以很容易地缩短它们而不会失去可读性:

start_number   -> startNum/start_num
stop_number    -> stopNum/stop_num
input_number   -> inputNum/input_num
rotation_units -> rotUnits/rot_units
direction      -> direction

这会导致减少:

def __init__(self, start_number, stop_number, input_number, rotation_units, direction):
def __init__(self, start_num, stop_num, input_num, rot_units, direction):
def __init__(self, startNum, stopNum, inputNum, rotUnits, direction):

除此之外,您可以使用\作为延续字符(或者在这种情况下使用 newline ),但我倾向于不这样做,因为它看起来像 uglier 对我而言,我喜欢在阅读时尽可能多地在屏幕上显示代码。