Classes - instance/class, methods/members, inheritance and be st practices

Evan Jones EvanJ at eyron.com
Thu May 3 08:05:50 EDT 2001


Thanks Alex and Rainer,  that's helped a lot.  
Cheers,
Evan

> -----Original Message-----
> From:	Alex Martelli [SMTP:aleaxit at yahoo.com]
> Sent:	Wed, May 02, 2001 5:30 PM
> To:	python-list at python.org
> Subject:	Re: Classes - instance/class, methods/members, inheritance
> and best practices
> 
> "Evan Jones" <EvanJ at eyron.com> wrote in message
> news:mailman.988812244.30750.python-list at python.org...
> > Hiya,
> >
> > I have just started learning Python and was just hoping someone could
> help
> > set things straight in my head on user defined classes.
> >
> > So far as I understand from the docs an instance variable should be
> > initialized by the __init__ method;
> 
> Good practice, yes.
> 
> > an instance method is simply defined in
> > the class definition; class variables are added to the class definition
> but
> > there are no class methods.
> 
> Essentially right.
> 
> > In addition I understand that there is no
> > 'constructor' chaining, so that if you are inheriting from some base
> > classes, when you instantiate the super class the __init__ methods don't
> get
> > called from the sub classes.
> 
> Not _automatically_ -- you have to do the calling yourself.
> 
> > So what I have been doing is to create each class in a file of its own
> and
> > add function definitions to that file to act as 'class' methods, and
> 
> Quite acceptable, I think.  There are of course alternatives, e.g.
> http://www.activestate.com/ASPN/Python/Cookbook/Recipe/52304.
> 
> > constructor chaining I have been doing by hand like:
> >
> > class Base:
> > __init__(self):
> > self.init_Base()
> >
> > init_Base(self):
> > #etc ...
> 
> This seems slightly too complicated: you could just do your
> initialization in __init__ here.  (You're also missing the
> 'def's, but I guess that's a minor oversight).
> 
> 
> > class Super(Base):
> > __init__(self):
> > self.init_Super()
> >
> > init_Super(self):
> > self.init_Base()
> > #etc ...
> 
> Again, Super's __init__ could just do the initialization
> itself, and start with a call to Base's own init.  I.e.:
> 
> class Super(Base):
>     def __init__(self):
>         Base.__init__(self)
>         # rest of Super's initialization
> 
> 
> I suspect the one thing you're missing here is that you
> CAN call a method on a class object, as long as you pass
> an instance object (of that class or any subclass thereof)
> as its first argument.
> 
> 
> > So if any one could correct me if I'm wrong, or let me know what the
> > accepted best practices for 'class methods' and 'constructor chaining'
> are,
> > I'd be very grateful.
> 
> I think your arrangement, modified to use direct calls to
> Base.__init__ &c and do the initialization in __init__, is
> likely to be best, because it's simplest.  Again there are
> alternatives, see e.g.
> http://www.activestate.com/ASPN/Python/Cookbook/Recipe/52236
> 
> 
> Alex
> 
> 
> 
> -- 
> http://mail.python.org/mailman/listinfo/python-list




More information about the Python-list mailing list