This is a question that keeps recurring in all of my programming, python and otherwise. I really like to keep my code under 80 chars if at all possible/not horribly ugly. In a l
Some people were citing the Rectangle class as a poor example. This example in the pep8 is not the only way to do this.
Original:
class Rectangle(Blob):
def __init__(self, width, height,
color='black', emphasis=None, highlight=0):
if (width == 0 and height == 0 and
color == 'red' and emphasis == 'strong' or
highlight > 100):
raise ValueError("sorry, you lose")
if width == 0 and height == 0 and (color == 'red' or
emphasis is None):
raise ValueError("I don't think so -- values are %s, %s" %
(width, height))
Blob.__init__(self, width, height,
color, emphasis, highlight)
This is how I would write it.
class Rectangle(Blob):
def __init__(self, width, height, color='black', emphasis=None,
highlight=0):
if (width == 0 and height == 0 and color == 'red' and
emphasis == 'strong' or highlight > 100):
raise ValueError("sorry, you lose")
if width == 0 and height == 0 and (color == 'red' or
emphasis is None):
msg = "I don't think so -- values are %s, %s" % (width, height)
raise ValueError(msg)
Blob.__init__(self, width, height, color, emphasis, highlight)
The reason being is:
%
is deprecated since 3.1).