Adding multiple constructors on one class

Hello python team! you should consider adding the ability to create multiple constructors on one class like c# and Java it is really useful. *Example*: i want a constructor takes 1 argument and other constructor takes 2 and another one takes none in the same class. so when i want to create an object i got a lot of options. i know i can do it by given a default value to the attribute but it will be better with multiple constructors

On Mon, May 6, 2019 at 6:09 AM Zakariae Saih <zakasaih@gmail.com> wrote:
More generally: You want to be able to have multiple functions with the same name, and have something distinguish them by their signatures. That's a difficult thing to do in Python, because there are so many complexities (keyword arguments, a habit of duck typing, etc), but not impossible. What you're looking for is sometimes called "multiple dispatch", and you can find some packages on PyPI that offer this feature - usually with some restrictions, eg distinguishing functions by positional args only, or requiring an actual isinstance check rather than "thing that can be added to an integer". Have a look at some of them and see if they do what you want. ChrisA

On Sun, May 05, 2019 at 09:08:42PM +0100, Zakariae Saih wrote:
Guido's time machine strikes again -- classes can have any number of constructors, and we've had the ability to do this since at least Python 2.2 if not earlier. For example: * dict has the standard constructor dict(...) and an alternate constructor dict.fromkeys(...); * datetime, date and time objects have lots of alternate constructors, such as datetime.fromtimestamp, datetime.now, datetime.combine, datetime.strptime and more.
And indeed you are correct -- you can already do this, and sometimes it is better with multiple constructors. -- Steven

Steven D'Aprano wrote:
Although I think the OP wants all the constructors to have the same name, which is more difficult to achieve in Python. Personally I'm not a fan of that kind of overloading -- I prefer different functions to have different names, so I'm happy with the status quo. -- Greg

On Mon, May 6, 2019 at 6:09 AM Zakariae Saih <zakasaih@gmail.com> wrote:
More generally: You want to be able to have multiple functions with the same name, and have something distinguish them by their signatures. That's a difficult thing to do in Python, because there are so many complexities (keyword arguments, a habit of duck typing, etc), but not impossible. What you're looking for is sometimes called "multiple dispatch", and you can find some packages on PyPI that offer this feature - usually with some restrictions, eg distinguishing functions by positional args only, or requiring an actual isinstance check rather than "thing that can be added to an integer". Have a look at some of them and see if they do what you want. ChrisA

On Sun, May 05, 2019 at 09:08:42PM +0100, Zakariae Saih wrote:
Guido's time machine strikes again -- classes can have any number of constructors, and we've had the ability to do this since at least Python 2.2 if not earlier. For example: * dict has the standard constructor dict(...) and an alternate constructor dict.fromkeys(...); * datetime, date and time objects have lots of alternate constructors, such as datetime.fromtimestamp, datetime.now, datetime.combine, datetime.strptime and more.
And indeed you are correct -- you can already do this, and sometimes it is better with multiple constructors. -- Steven

Steven D'Aprano wrote:
Although I think the OP wants all the constructors to have the same name, which is more difficult to achieve in Python. Personally I'm not a fan of that kind of overloading -- I prefer different functions to have different names, so I'm happy with the status quo. -- Greg
participants (4)
-
Chris Angelico
-
Greg Ewing
-
Steven D'Aprano
-
Zakariae Saih