newsgroups at jhrothjr.com
Wed Jun 9 12:53:30 CEST 2004
"Sergey Krushinsky" <lunarium at comail.ru> wrote in message
news:mailman.746.1086771964.6949.python-list at python.org...
> Hello all,
> Is there a common way to emulate constructor overloading in Python class?
> For instanse, I have 3 classes:
> 1/ Polar - to hold polar coordinates;
> 2/ Cartesian - to hold cartesian coordinates;
> 3/ Coordinates3D, which holds synchronized instances of the both in
> __p__ and __c__ fields respectively.
> I want to design Coordinates3D so that when instantiated with Polar
> argument, self.__p__=argument passed to constructor, and self.__c__ is
> calculated. When argument is Cartesian, self.__c__=argument, and
> self.__p__ is calculated. Runtime type checking works, but maybe there
> is a better way?
Depends on what you think is "better." Checking the paramter
types at run time is the clearest way of doing it. The only other
way I know is to use a static method as a constructor, instantiate
an instance of object(), change its class and initialize it to suit
The biggest problem with that is that it's totally non-obvious
unless your team does it a lot, and you've got decent naming
conventions so you know what's happening.
> Thanks in advance,
More information about the Python-list