[Tutor] Challenge supporting custom deepcopy with inheritance
W W
srilyk at gmail.com
Mon Jun 1 22:18:37 CEST 2009
On Mon, Jun 1, 2009 at 8:59 AM, Alan Gauld <alan.gauld at btinternet.com>wrote:
>
> "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
>
That does make it problematic... although I suppose checking the type would
be a workaround - still, not simple or beautiful. Has this been introduced
as a PEP?
-Wayne
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090601/f4aa8311/attachment-0001.htm>
More information about the Tutor
mailing list