''' Scale a rectangle from its lower left corner '''assertlen(rect) ==4, "Rectangle must have four coordinates x1, y1, x2, y2"x1, y1, x2, y2=rectassertx1<=x2, "Invalid X coordinates"asserty1<=y2, "Invalid Y coordinates"return (x1, y1, x2*factor, y2*factor)
The text was updated successfully, but these errors were encountered:
I agree with you. I am not sure it is necessary to have such complex function. However, the name of the function scale_rectangle looses its meaning with your simplified version. I guess the idea was also to have a meaningful function (in terms of content). I labeled this issue as both enhancement and discussion to get more inputs from others.
I believe that neither the summing numbers nor the rectangle example show good practice for a few reasons:
Why would we teach someone, that only positive values can be summed? It is very pythonic, that a sum function can sum up any kind of object for which the + operator is defined.
A rectangle is defined by any two points in the coordinate system. Swapping the order will not change the rectangle. If the function cannot handle this, the first place to mention this would be in the docstring and as a result of the definition the coordinates should be checked. If they are unordered, a ValueError should be raised.
A good example would be:
Check the length of the list, which describes the rectangle. If the length is longer than 4, it should raise an IndexError (a shorter list would cause the same error in the line x1, y1, x2, y2 = rect).
I actually find it quite hard to come up with a good example myself, because most errors are raised in the function anyways without manual checking before. From my experience the typical case is checking the number of arguments.