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