[Tutor] Challenge supporting custom deepcopy with inheritance

W W srilyk at gmail.com
Mon Jun 1 23:50:05 CEST 2009

On Mon, Jun 1, 2009 at 3:55 PM, Alan Gauld <alan.gauld at btinternet.com>wrote:

> "W W" <srilyk at gmail.com> wrote
>> 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?
> Type checking is the problem. Until Python can distinguish methods based
> on types (which introduces other issues)  thisi is difficult with the
> C/Java style.
> We can fake the Delphi style by using a default constructor and then just
> calling the "constructors" after initialisation:
> class C:
>        def __init__(): pass
>       @constructor
>       def LoadFromFile(fname, count): ...
>      @constructor
>      def Create(valueTuple):...
> c = C()
> c.LoadFromFile(fn, cnt)
> But its two lines not one... :-(
> And so far as I know it has not been PEPd although I'm sure it has
> been discussed.

I'm not sure how types are implemented in the underlying C, but it seems
that it should be a somewhat trivial addition. I mean, type checking is
already built-in to python, i.e. type('a')  o/p: <type 'str'>, so just
building a handler specific to the __init__ method, or modifying it if it's
already unique, should be able to take care of it.

I guess one way you could try to parse it on your own is build a list of
types: f1 = [type(''), type(1), type(())], f2 = [type([]), type(1)]]  and
compare the types of arguments provided from *args. I suppose really, one
could go so far as to build a dict of lists with the lengths:

spam = {1:[[type(''))],], 2:[[type([]), type(())], [type(1), type(1.0)]

then compare then lengths of args first.

That's a lot of work, though :P
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090601/d8c0a0ed/attachment.htm>

More information about the Tutor mailing list