[Python-bugs-list] [ python-Bugs-441314 ] __init__ not callable if non existant.

noreply@sourceforge.net noreply@sourceforge.net
Sat, 14 Jul 2001 11:24:12 -0700


Bugs item #441314, was opened at 2001-07-14 11:08
You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=441314&group_id=5470

Category: Python Interpreter Core
Group: Feature Request
Status: Closed
Resolution: Accepted
Priority: 5
Submitted By: Chris Cogdon (chriscog)
Assigned to: Guido van Rossum (gvanrossum)
Summary: __init__ not callable if non existant.

Initial Comment:
"Good OO programming" dictates that one should never 
rely on the base class you are inheriting from to not 
change. To this end, if I was to have a class C 
inheriting from B, and was to write my own __init__ 
method, then I should also call B's __init__ method 
too. Viz:

class C (B):
    def __init__ ( self, some_args_for_C ):
        B.__init__ ( self )

Unfortuantely, if B does not have a __init__ method, 
then instantiating C will cause an AttributeError. 
This makes sense, of course, but it does mean that if 
B is ever changed to include a __init__ method, then 
/every/ class that derives from B, directly or 
indirectly, and has their own __init__ methods, have 
to be modified to call their immediate base class' 
__init__ method. This is Bad.

The rule should be: if you inherit, and have your own 
__init__ method, then you should call your base 
class' __init__ method in your own __init__. The 
python interpreter should be modified to allow this 
'forsight' in OO programming.

Suggest somehow special-casing calls to __init__ so 
that the exception is suppressed, and is simply a 
NOOP. I appreciate that, in reality, it's getattr 
that needs to be special-cased, and have it return a 
NOOP function.


----------------------------------------------------------------------

>Comment By: Chris Cogdon (chriscog)
Date: 2001-07-14 11:24

Message:
Logged In: YES 
user_id=67116

Workaround, and long-term solution work for me. Thanks for 
the speedy reply! :)


----------------------------------------------------------------------

Comment By: Guido van Rossum (gvanrossum)
Date: 2001-07-14 11:15

Message:
Logged In: YES 
user_id=6380

Good point.

In the short term, it's best to make sure that the author of
B defines an empty __init__.

In the long term, I'm working on redoing the class
definition machinery to allow subclassing built-in types,
and there will be a universal base class, 'object', whose
__init__ you will automatically inherit.  See PEP 253
(warning: unfinished!).

I'm closing this issue, because I don't think I want to do
something else about this now.

----------------------------------------------------------------------

You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=441314&group_id=5470