[Python-ideas] Adding multiple constructors on one class
Steven D'Aprano
steve at pearwood.info
Sun May 5 19:54:02 EDT 2019
On Sun, May 05, 2019 at 09:08:42PM +0100, Zakariae Saih wrote:
> Hello python team!
> you should consider adding the ability to create multiple constructors on
> one class like c# and Java it is really useful.
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.
> *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
And indeed you are correct -- you can already do this, and sometimes it
is better with multiple constructors.
--
Steven
More information about the Python-ideas
mailing list