[Python-Dev] subclassing builtin data structures
Serhiy Storchaka
storchaka at gmail.com
Sat Feb 14 07:01:40 CET 2015
On 14.02.15 03:12, Ethan Furman wrote:
>> 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.
In case of defaultdict (when dict would have to have __add__ and like)
either __make_me__ == dict (then defaultdict's methods will return
dicts) or it will be instance method.
def __make_me__(self, other):
return defaultdict(self.default_factory, other)
More information about the Python-Dev
mailing list