2 ms·
Remember that duck typing + default values + args + kwargs is usually more idiomatic than function overloading in python. Don't try to code in python like you
by BiteCode_dev 7y ago
Remember that duck typing + default values + args + kwargs is usually more idiomatic than function overloading in python.
Don't try to code in python like you code in another language.
Also, think about what your API is conveying. Overloading may make it less clear.
The snippet from this article is a good example of when not to do it:
def area (l, b):
return length * breadth;
def area(r):
return 3.14 * radius * radius
Here, you have 2 functions that do 2 fundamentally different things. One is calculating an area for a circle. The other one is for a rectangle. When you have 2 different signatures, it's often a sign the process is very different, and you should signal that in your API.
If you scan code using those, your brain cannot quickly parse it. It needs to process "area(), ok but of what, well, it has only x param, so I guess this is for z". The overloading gimmick has little value for the reader, but a real cost (not to mention in python it actually slows the run time)
The better solution is simply to make it explicitly 2 functions with good names:
def rectangle_area (l, b):
return length * breadth;
Or, most likely, put them in separated namespaces (using a module or a class), which does the same.
import circle
circle.area(r)
It's very easy to abuse overloading because it tickles our need for refactoring. But it's a tool to solve a problem, not an end by itself.
- throwaway_pdp09 7y agoThe first part seems the wrong way to do it (treating it as an example). You would presumably overload area() on the object type, not on individual parameters representing the object's geometry. 'circle.area(r)' is even weirder - extracting radius just doesn't make sense here. <SomeGeometricObject>.area() is right AFAICS. Which I think is what you're getting at anyway. But IMO overloading used right is cleaner than not having overloading.
- BiteCode_dev 7y ago> 'circle.area(r)' is even weirder It's just putting the original area() function in a module, not making it a method of a class. It's the simplest solution you can have, ever. Adding an object is already more complex, and complexity needs to be justified. Just KISS if you can.
- oefrha 7y agoThere's really nothing wrong with having two different signatures, it's a pretty pythonic way to do things. It's harder to document for sure, but other than that hardly a problem in many cases. This post unfortunately reads like one of those Java programmer discovers Python (except not really) posts, but for C++. In general if you find yourself inventing a new paradigm for a 30-year old language you just started working with, it's reason enough to doubt you're doing something suboptimal. However, in this case the author is actually very familiar with Python, so I'm kinda confused.