[Tutor] rationale for nested classes?

Alan Gauld alan.gauld at btinternet.com
Mon Aug 17 21:27:28 CEST 2009


"Mac Ryan" <quasipedia at gmail.com> wrote in message 
news:1250529165.18338.24.camel at jabbar...
> On Fri, 2009-08-14 at 11:26 -0400, Serdar Tumgoren wrote:
>> Hi everyone,
>>
>> I was wondering if there's anyone who can

> I would use a nested class to create a particular data structure which
> represent a "sub-unit" of the mother class. For example if I had a class
> representing a table (with methods like add, delete, sort, etc...) I
> might wish to create a subclass that represent a record and use
> instantiation of that sub-class as my main way to handle data.

Thats a fair enough idea except there is no advantage in defining
the class as a nested class. It would IMHO be better at the module
level where it would be easier for a client to subclass it for different
record types. It would be very useful to be able to subclass a record
in this way so it would not be a candidate for hiding inside another
class.

OTOH the coupling between table and record makes an equally
strong case for having both record and table classes defined in
the same module.

> way I could enforce behaviours like "not null" or "default" from within
> the datastructure itself (for example in the __init__ method) rather
> than enforcing a logic from outside (i.e. the "mother class").

But those features are features of the record not of the table.
A table is simply a collection of records. Most of the functionality
should be in the record, the table should only need to insert,delete,
search, sort etc. And those methods should be written to use
polymorphism to be record independent

> I believe classes/objects are also the most elegant way to implement
> singletons, so maybe if you need a singleton for each object generated
> with the "mother class", maybe that is also an occasion in which a
> subclass comes handy.

I have no idea what you mean by that bit. Classes can be used for
singletons but I don;t see where nested classes come into it.

Nested classes should be used where you don't expect anyone
outside the outer class to ever want to do anything with the inner
class. (In Python this is just a statement of intent since the access
is available to anyone who wants it)

> Finally, I somewhere read that embedded declarations are much faster
> than external ones in being referenced.

Doesn't apply here.

> would be very happy if any of the tutors would comment on what above,
> even if it is for disagree with it! :)

Be very happy! :-)
More seriously, I suspect from your comments that you too are
getting confused about the difference between inheritance and nesting.
nesting is about hiding the class name and definition. Inheritance is
about subclassing. They are very different concepts. Nesting tends
to make subclassing more difficult (albeit in Python only trivially so)

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/ 




More information about the Tutor mailing list