[Tutor] rationale for nested classes?

Alan Gauld alan.gauld at btinternet.com
Fri Aug 14 19:40:42 CEST 2009


"Serdar Tumgoren" <zstumgoren at gmail.com> wrote

> I was wondering if there's anyone who can offer a use case/rationale
> for nested class?

It provides another level of name hiding but personally I don;t think there
is much benefit. If you keep the related classses in the same module
that should be good enough IMHO.

But the fact that you can have nested classes isn't a bad thing,
it keeps Python consistent, but its not too userful in my view.

> class A(object):
>    class B(object):

> My initial thought was that perhaps they inherit the behavior of
> parent classes, but that apparently is not the case:

Nope, inheritance is done by inheritance! :-)

> Are there specific situations when nested classes come in handy
> (perhaps for grouping conceptually related classes that don't share
> attributes?).

Really only used if you have a special kind of thing that is only ever
going to be used inside your outer class. But its only an indicator
because you can still use A.B to access the class from outside!
So its a mild deterrant not a real barrier.

> Or is it typically better to keep all classes on the same level of a
> heirarchy, and then just use inheritance to establish parent-child
> relationships where appropriate?

Yes, but nesting really has nothing to do with inheritance.

HTH,

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




More information about the Tutor mailing list