no return values for __init__ ??

Gordon McMillan gmcm at hypernet.com
Sun Jan 9 21:11:25 EST 2000


Helge Hess wrote:

> Gordon McMillan wrote:
> > Is inheritance a matter of taste?
> 
> Strange questions ?!

Questioning a strange assertion.

> > You claim to have patched your interpreter. Try this:
> > 
> > class A:
> >   def __init__(self):
> >     pass
> > 
> > class B:
> >   def __init__(self):
> >     return A()
> > 
> > class C(B):
> >   def __init__(self):
> >     self = B.__init__(self)
> >   def method(self):
> >     print "C.method called"
> > 
> > c = C()
> > c.method()
> > 
> > Produces an AttributeError, doesn't it?
> 
> (First, *no*, it doesn't, since C.__init__ doesn't return a new
> object, but only reassigns 'self').

And how does "method" become an available to 'self'? 

> Why should I do the above ? 

To prove that returning a value from __init__ is a sane thing to 
do.

> It's a completly constructed example.

Of course. I don't have an interpreter which allows __init__ to 
return a value, so what else could I produce?

> It's similiar to this:
> 
> class A:     def __init__(self):  pass
> class B(A):  def __init__(self):  return self.method()
>
> Produces an AttributeError as well ...

Of course it does. You didn't define "method". So yours is a 
programmer error. I did define it.
 
> The problem in your example is that you are misusing return
> values, eg
>
>   class C(B):
>     def __init__(self):
>       s = B.__init__(self)
>       if (s is not None) & (s is not self): return s
> 
> will work perfectly.

I didn't realize that return values were something one could 
abuse. Fine. Add those lines to the example.

And C().method() will produce what result with your interpreter?

Or by insisting that __init__ is "just another method", have 
you succeeded in producing a special kind of class that looks, 
smells and tastes like "just another" class, except it may 
blow up on you at run time?

- Gordon




More information about the Python-list mailing list