<div dir="ltr">I think it works as Isaac explained if __make_me__ is an instance method that also accepts the calling class type.</div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Feb 13, 2015 at 8:12 PM, Ethan Furman <span dir="ltr"><<a href="mailto:ethan@stoneleaf.us" target="_blank">ethan@stoneleaf.us</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">On 02/13/2015 02:31 PM, Serhiy Storchaka wrote:<br>
> On 13.02.15 05:41, Ethan Furman wrote:<br>
>> So there are basically two choices:<br>
>><br>
>> 1) always use the type of the most-base class when creating new instances<br>
>><br>
>> pros:<br>
>> - easy<br>
>> - speedy code<br>
>> - no possible tracebacks on new object instantiation<br>
>><br>
>> cons:<br>
>> - a subclass that needs/wants to maintain itself must override all<br>
>> methods that create new instances, even if the only change is to<br>
>> the type of object returned<br>
>><br>
>> 2) always use the type of self when creating new instances<br>
>><br>
>> pros:<br>
>> - subclasses automatically maintain type<br>
>> - much less code in the simple cases [1]<br>
>><br>
>> cons:<br>
>> - if constructor signatures change, must override all methods which<br>
>> create new objects<br>
><br>
> And switching to (2) would break existing code which uses subclasses with constructors with different signature (e.g.<br>
> defaultdict).<br>
<br>
</span>I don't think defaultdict is a good example -- I don't see any methods on it that return a new dict, default or<br>
otherwise. So if this change happened, defaultdict would have to have its own __add__ and not rely on dict's __add__.<br>
<span class=""><br>
<br>
> The third choice is to use different specially designed constructor.<br>
><br>
> class A(int):<br>
><br>
</span>> --> class A(int):<br>
<span class="">> ... def __add__(self, other):<br>
> ... return self.__make_me__(int(self) + int(other))<br>
><br>
> ... def __repr__(self):<br>
> ... return 'A(%d)' % self<br>
<br>
</span>How would this help in the case of defaultdict? __make_me__ is a class method, but it needs instance info to properly<br>
create a new dict with the same default factory.<br>
<br>
--<br>
~Ethan~<br>
<br>
<br>_______________________________________________<br>
Python-Dev mailing list<br>
<a href="mailto:Python-Dev@python.org">Python-Dev@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-dev" target="_blank">https://mail.python.org/mailman/listinfo/python-dev</a><br>
Unsubscribe: <a href="https://mail.python.org/mailman/options/python-dev/mistersheik%40gmail.com" target="_blank">https://mail.python.org/mailman/options/python-dev/mistersheik%40gmail.com</a><br>
<br></blockquote></div><br></div>