[Tutor] Challenge supporting custom deepcopy with inheritance

Alan Gauld alan.gauld at btinternet.com
Mon Jun 1 15:59:11 CEST 2009


"W W" <srilyk at gmail.com> wrote

>> ( Multiple constructors (or factory methods) is one feature I  would 
>> like
>> to see added to Python! )
>
> Wouldn't it be possible to create sort of a... bastardization? i.e.
>
> def __init__(self, *args):
>    if len(args) == 0:
>        #do something
>    if len(args) == 1:
>       #do something else
>
> Or would that create more problems than is worth it?

Thats the standard workaround but its not clean and certainly
not self documenting. Its also not reliable becaause thre is nothing
to stop someone calling it with the first argument of the 2 arg case
(a filename maybe?) and then it gets treated as a single arg case
( a tuple of values say?) Oops!

Whereas in C++/Java style you could say

class C:
    @constructor
    def C(fname, count);...

    @constructor
    def C(valueTuple):....

etc.

Or in Delphi style:

class C:
     @constructor
     def LoadFromFile(fname, count): ...
     @constructor
     def Create(valueTuple):...

Personally I prefer the Delphi style sincve it makes the constructor
call explicit and adds to the documentation.

Alan G 




More information about the Tutor mailing list