Python – Good practice for returns in Python

design-patternspython

I was recently working on some prototype code in Python. The code worked great, then I realized that I needed a little more feedback from one of my functions so I changed the return statement from

return x

to

return (x,y)

Then going back to refactor existing code I realized that unless I search for invocation of this function across entire of the code base I won't know of any problems until run-time (and if this is some marginal case) barely ever and risking the program crashing due to uncaught exception resulting in operations on mismatched types. I contrast this with statically typed languages, where at compile time I get told about all places where the return doesn't mismatches expected type.

So I was wondering whether there is some conventional wisdom associated with the return types in Python to avoid this grepping through code nonsense.

P.S> Obviously better design patterns would solve this problem or not using duck typed language, but sometimes one comes across a case when you got to change the return type.

Best Answer

PyCharm (the community version is FOSS) will do a pretty good job of finding all calls to your function (Alt-F7), so you can easily see how it's called. Of course, being a dynamic language it can't untangle truly horrible code, but your use case should be fine.

Another mechanism which may fit your case better (since returning two things is a common way for code to smell of missing objects) is to return an object with two attributes.