Inheriting from object

Bengt Richter bokr at oz.net
Sat Jul 2 23:09:28 EDT 2005


On Sat, 02 Jul 2005 14:17:32 -0400, Peter Hansen <peter at engcorp.com> wrote:

>Bengt Richter wrote:
>> BTW, there's something about referring to type(self) by its not
>> always dependably bound (though usually global) name that bothers me.
>> 
>> I wonder if the above common use of super could be implemented as a property of object,
>> so you'd normally inherit it and be able to write
>>     self.super.__init__(*args, **kwargs)  # (maybe spell it self.__super__.__init__(...) I suppose)
>> 
>> I.e., self.__super__ would effectively return the equivalent of
>>     super(type(self), self)
>
>This doesn't work: type(self) is always the type of the instantiated 
>(child) class, not the type of the class at whatever level of the class 
>hierarchy the __init__() calls currently have reached.
>
D'oh, you're right, that was too easy a property definition ;-/
I still don't like having to use a global name to get access to the
current lexical scope object though. But I can't think of a decent hack
right now to get around all that, and I can't spend too much time ;-/

>In other words, with these definitions:
>
>class A(object):
>   def __init__(self):
>      super(type(self), self).__init__()  # does not do what you want
>
>class B(A):
>   def __init__(self):
>      super(type(self), self).__init__()	# works okay here
>
>if you do "b = B()", the first __init__ will work (i.e. B's __init__ 
>will find and call A.__init__), but the next one won't (i.e. A.__init__ 
>will now try calling A.__init__ recursively, giving you an eventual 
>stack overflow).
>
>-correcting-bengt-richter-on-such-arcana-is-always-dangerously y'rs,
>  Peter
No danger at all, I'm grateful ;-)

Regards,
Bengt Richter



More information about the Python-list mailing list