no return values for __init__ ??

Helge Hess helge.hess at mdlink.de
Mon Jan 10 07:36:53 EST 2000


Hi,

Gordon McMillan wrote:
> Helge Hess wrote:
> > > 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'?

'c' is of class C() because __init__ did no return a new object
(B.__init__ does, but this is thrown away by not returning 'self').

> > Why should I do the above ?
> 
> To prove that returning a value from __init__ is a sane thing to
> do.

I think we are getting into a loop here. You can do insane stuff with
any construct, returning a value from __init__ is no exception of
course.
In practice the above construct won't appear and is a semantic
programming error like my example below.

> > 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.

You did a programming error as well by not returning the correct object.

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

AttributeError due to programming error.

> 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?

If you like you can 'blow up' your runtime, like you can do with any
other factory approaches I was shown. The question is why I should want
to blow up my runtime - doesn't make a lot of sense to me ..

We should really stop discussing the topic now, it is getting weird.

Helge
-- 
SKYRIX-OS  Web Application Environment - http://www.skyrix.com
SKYRIX-IAS Groupware Application Suite - http://www.skyrix.com




More information about the Python-list mailing list