News for hackers. Stuff that matters.
Re:Implementing Function Overloading in Python by anentropic on Thursday March 26, 2020 @07:44AM (#22692528)
This is pretty horrible.

And you could achieve the same thing with built-in functools.singledispatch by designing your API better

e.g. the example given is:

    int area(int length, int breadth) {
      return length * breadth;
    }

    float area(int radius) {
      return 3.14 * radius * radius;
    }
What if you need to calculate the area of another shape that uses a single integer value as the parameter?

With singledispatch and some types:

    import math
    from functools import singledispatch
    from typing import NamedTuple

    class Rectangle(NamedTuple):
        length: int
        breadth: int

    class Circle(NamedTuple):
        radius: int

    @singledispatch
    def area(val) -> None:
        raise TypeError(val)

    @area.register
    def _(rectangle: Rectangle) -> int:
        return rectangle.length * rectangle.breadth
    
    @area.register
    def _(circle: Circle) -> float:
        return math.pi * math.pow(circle.radius, 2)
Re:Implementing Function Overloading in Python by BiteCode_dev on Thursday March 26, 2020 @08:55AM (#22693027)
Again this is over-engineering. Put functions in modules to namespace them and be done with it.

If you really want types, use classes. It removes the need for single dispatch, creates a self documenting namespace and saves you an import when you want to do the calculation.

    import math
    from dataclasses import dataclass

    @dataclass
    class Rectangle:
        length: int
        breadth: int
        def area(self) -> int:
            return self.length * self.breadth

    @dataclass
    class Circle:
        radius: int
        def area(self) -> float:
            return math.pi * self.radius ** 2

@dataclass is of course not mandatory, and just make the code shorter.

We are talking about very simple things here. No need to add complexity to them.

Re:Implementing Function Overloading in Python by anentropic on Thursday March 26, 2020 @11:41AM (#22694641)
Yes I agree, if I was writing this for my own project I'm pretty sure I would do what you have done and use classes with an `area` method. (FWIW I think the dataclass approach is nicer for this example than namespacing separate `area` functions using modules)

You could define it as a typing.Protocol too.

My point was just that, even if for some reason you prefer function overloading, the restrictions needed to fit within the constraints of functool.singledispatch arguably lead to cleaner code than the complicated mess in the OP article.

This would look messy if you implemented as just a single function with variable no of kwargs that you sniffed in the body, and I don't think applying function overloading alone as OP has done solves the underlying problem.

Re:Implementing Function Overloading in Python by BiteCode_dev on Thursday March 26, 2020 @12:44PM (#22695379)
> My point was just that, even if for some reason you prefer function overloading, the restrictions needed to fit within the constraints of functool.singledispatch arguably lead to cleaner code than the complicated mess in the OP article.

Indeed. Less is more sometimes.

Re:Implementing Function Overloading in Python by jakearmitage on Thursday March 26, 2020 @11:43AM (#22694676)
Didn't know about @dataclass until now. Thank you.
Re:Implementing Function Overloading in Python by oefrha on Thursday March 26, 2020 @12:53PM (#22695470)
See also attrs http://www.attrs.org/ which is one of the main inspirations of py37 dataclass and is more feature-rich.
Re:Implementing Function Overloading in Python by BiteCode_dev on Thursday March 26, 2020 @12:43PM (#22695374)
Python 3.7+ only.
Re:Implementing Function Overloading in Python by harel on Thursday March 26, 2020 @12:08PM (#22695000)
Came here to say the same thing - so take my upvote sir.