[Tutor] Modify inherited methods

Walter Wefft walterwefft at googlemail.com
Wed Apr 28 12:20:34 CEST 2010


spir ☣ wrote:
> On Wed, 28 Apr 2010 07:53:06 +0100
> Walter Wefft <walterwefft at googlemail.com> wrote:
> 
>> Steven D'Aprano wrote:
>>  > And for guru-level mastery, replace to call to dict.__init__ with ... 
>> nothing at all, because dict.__init__ doesn't do anything.
>>  >
>>  >
>>  >
>>
>> (Sorry, should have sent to list).
>>
>> I don't understand this - it must do something:
>>
>> class MyDict1(dict):
>>
>>     def __init__(self, *args, **kw):
>>         pass
>>
>> class MyDict2(dict):
>>
>>     def __init__(self, *args, **kw):
>>         dict.__init__(self, *args, **kw)
>>
>>
>> d = MyDict1(y=2)
>> print d
>> {}
>>
>> d = MyDict2(y=2)
>> print d
>> {'y': 2}
>>
>> d = MyDict1({'x': 3})
>> print d
>> {}
>>
>> d = MyDict2({'x': 3})
>> print d
>> {'x': 3}
>>
>> Behaviour is different depending on whether you call the superclass 
>> __init__ or not.
>>
>> ?
> 
> Hem... this is a rather obscure point (I personly have it wrong each time I need to subtype builtin types). Maybe you find some enlightenment in the following code:
> 
> ===============================
> class MyDict0(dict):
>     pass
> class MyDict1(dict):
>     def __init__(self, *args, **kw):
>         pass
> class MyDict2(dict):
>     def __init__(self, *args, **kw):
>         dict.__init__(self, *args, **kw)
> ===============================
> 
> d0 = MyDict0(a=1) ; d1 = MyDict1(a=1) ; d2 = MyDict2(a=1)
> print d0,d1,d2 # ==> {'a': 1} {} {'a': 1}
> 

You reiterate my point. To say that dict.__init__ can be omitted in a 
subclass's __init__ with no effect, is not a correct statement.



More information about the Tutor mailing list