Python code formatting

In response to my other question, someone suggested I avoid long lines in the code and use the PEP-8 rules when writing Python code. One PEP-8 rule suggested avoiding lines longer than 80 characters. I modified my code to fulfill this requirement without any problems. However, changing the next line in the manner shown below violates the code. Any ideas why? Does this relate to the fact that what follows the return command should be on the same line?

Line longer than 80 characters:

  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) 

The line was changed using the Enter key and Spaces if necessary:

  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) 
+4
source share
4 answers

Python does not allow you to end a line inside such an expression; The simplest workaround is to end the line with a backslash.

 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) 

In this case, the backslash must be the last character in the string. Essentially, this means "ignoring the fact that there is a new line." Or, in other words, you are avoiding a new line, as this will usually be a significant break.

You can avoid a meaningful new line with a backslash at any time. That would be stupid, but you could even do

 def foo(): return \ 1 

so foo() will return 1. If you did not have a backslash, 1 will itself cause a syntax error.

+4
source

A multi-line string will be more readable:

 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) 

To preserve visually meaningful indentation levels, use 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)) 
+13
source

You can solve the problem by putting the expression in parentheses:

 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)) 

However, I would think about writing it more like this: (unverified code)

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

This requires a little extra customization, but a data-based approach (with a good dose of vertical alignment) is easy to obtain and modify as the project progresses. And this indirectly fixes the problem of long lines of code.

 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) 
+2
source

Source: https://habr.com/ru/post/1309174/


All Articles