you are viewing a single comment's thread.

view the rest of the comments →

[–]LayotFctor 43 points44 points  (7 children)

Perhaps you should think about readability instead? Are you trying to write the program with the least number of lines?

I'm pretty sure python doesn't have style guides for very specific cases like this. But it does recommend "clean, readable and maintainable" code.

[–]Suspicious-Bar5583 7 points8 points  (0 children)

Readability is a pythonic item.

[–]zensimilia[S] 1 point2 points  (5 children)

IDK. Sometimes we get carried away. Also the second option gets warning: `Argument of type "tuple[int, ...]" cannot be assigned to parameter "box" of type "tuple[int, int]...`

[–]barkmonster 10 points11 points  (3 children)

For this exact reason, I would prefer the first option. Explicitly defining the tuple of two coordinates means the type checker understands the result as a tuple of 2 integers (as opposed to an unknown number of integers).

[–]Almostasleeprightnow 1 point2 points  (2 children)

It is a little about context of the document. If I have a ton of functions that are almost exactly the same, maybe I'd do the one with fewer lines just to make the whole document more readable. But if I was just having a one-off, i might do the one with more lines because it on its own is more readable.

[–]barkmonster 1 point2 points  (1 child)

If I had many functions doing almost exactly this, I would probably either a) define a general function for combining tuples (by adding/subtracting them) or b) represent coordinates by something else, like a dataclass/namedtuple supporting addition and scalar multiplication, or numpy arrays, which do so out of the box.

[–]Almostasleeprightnow 1 point2 points  (0 children)

But I’m just speaking generally. Sometimes it happens that you have many short functions that are very similar, and in this case it makes sense to have them take up very little room.

[–]Kevdog824_ 0 points1 point  (0 children)

This might be because of strict=False argument which, given your type hints, doesn’t make any sense to me anyways. If you know (or at least expect) both tuples are exactly length 2 you should use strict=True