2010-05-08 97 views
4

为了回应我的另一个问题,有人建议我避免在代码中使用很长的行,并在编写Python代码时使用PEP-8规则。 PEP-8规则之一建议避免超过80个字符的行。我改变了很多代码以符合这个要求,没有任何问题。但是,按照下面显示的方式更改以下行将破坏代码。任何想法为什么?是否与return命令后面的内容必须在一行中有关?Python代码格式化

线长于80个字符:

def __str__(self): 
    return "Car Type \n"+"mpg: %.1f \n" % self.mpg + "hp: %.2f \n" %(self.hp) + "pc: %i \n" %self.pc + "unit cost: $%.2f \n" %(self.cost) + "price: $%.2f "%(self.price) 

线通过使用Enter密钥和Spaces适当变更:

def __str__(self): 
    return "Car Type \n"+"mpg: %.1f \n" % self.mpg + 
        "hp: %.2f \n" %(self.hp) + "pc: %i \n" %self.pc + 
        "unit cost: $%.2f \n" %(self.cost) + "price: $%.2f "%(self.price) 
+0

首先摆脱字符串连接并使用%s替换,这将大大缩短行。像这样的东西。 'Car Type \ nmpg:%.1f \ n%d hp:%.2f \ npc:%i \ nunit cost:$%。2f \ nprice:$%。2f'%(self.mpg,self.hp,self .pc,self.cost,self.price),你甚至可以命名替换,使它更清晰你在做什么。 – 2010-05-08 04:04:00

+1

PEP建议非常差并且过时;将代码压缩到79列是一种很好的方法,因为PEP示例本身可以生动地展现出一些不可读的代码。 120列更合理,迫使更少的行被拆分。 80列屏幕*不是一个严重的开发环境。 – 2010-05-08 16:53:58

回答

4

Python不让你最终像的表达式内的线那;最简单的解决方法是用反斜杠结束该行。

def __str__(self): 
    return "Car Type \n"+"mpg: %.1f \n" % self.mpg + \ 
      "hp: %.2f \n" %(self.hp) + "pc: %i \n" %self.pc + \ 
      "unit cost: $%.2f \n" %(self.cost) + "price: $%.2f "%(self.price) 

在这种情况下,反斜线必须是行的最后一个字符。本质上,它意味着“忽略这里有一个换行符的事实”。换句话说,你正在逃避换行,因为它通常是一个重大的突破。

你可以在任何时候用反斜杠转义一个重要的换行符。这将是愚蠢的,但你甚至可以做

def foo(): 
    return \ 
    1 

使foo()将返回1.如果您没有有没有反斜杠,1本身导致语法错误。

+9

更好的解决方法是在表达式周围放置括号。 – 2010-05-08 04:04:05

13

多行的字符串将是更具可读性:

def __str__(self): 
    return '''\ 
Car Type 
mpg: %.1f 
hp: %.2f 
pc: %i 
unit cost: $%.2f 
price: $%.2f'''% (self.mpg,self.hp,self.pc,self.cost,self.price) 

为了保持视觉上有意义的缩进层次,使用textwrap.dedent

import textwrap 
def __str__(self): 
    return textwrap.dedent('''\ 
     Car Type 
     mpg: %.1f 
     hp: %.2f 
     pc: %i 
     unit cost: $%.2f 
     price: $%.2f'''% (self.mpg,self.hp,self.pc,self.cost,self.price)) 
+0

我更喜欢这种方法 – dzen 2010-05-08 09:12:04

+0

这种方法的问题是缩进。没有长字符串的缩进是很难看的。 – Dingle 2010-05-08 16:52:57

6

你能解决这个问题,通过把括号中的表达式:

def __str__(self): 
    return ("Car Type \n"+"mpg: %.1f \n" % self.mpg + 
      "hp: %.2f \n" %(self.hp) + "pc: %i \n" %self.pc + 
      "unit cost: $%.2f \n" %(self.cost) + "price: $%.2f "%(self.price)) 

但是,我会考虑写它更像这样的:(未测试的代码)

def __str__(self): 
    return """\ 
Car Type 
mpg: %(mpg).1f 
hp: %(hp).2f 
pc: %(pc)i 
unit cost: $%(cost).2f 
price: $%(price).2f """ % self.__dict__ 
+0

请注意,有'foo.bar'不等同于'foo .__ dict __ ['bar']'的原因。 – 2010-05-08 05:42:15

+2

@Mike强调“我会考虑”。如果我正在写'__str__',我可能知道它们是否等价。 – 2010-05-08 05:55:53

+0

'vars(self)'看起来比'self .__ dict__'更清洁(它也是这样) – jfs 2010-05-08 14:25:48

2

它需要一些额外的设置,但一个数据驱动的方法(垂直排列良好的剂量)很容易神交和修改作为项目的发展。它间接消除了长行代码的问题。

def __str__(self): 
    dd = (
     ("Car Type  %s", ''), 
     (" mpg:  %.1f", self.mpg), 
     (" hp:  %.2f", self.hp), 
     (" pc:  %i", self.pc), 
     (" unit cost: $%.2f", self.cost), 
     (" price:  $%.2f", self.price), 
    ) 

    fmt = ''.join("%s\n" % t[0] for t in dd) 
    return fmt % tuple(t[1] for t in dd)