[Tutor] rationale for nested classes?
bob gailer
bgailer at gmail.com
Fri Aug 14 19:21:39 CEST 2009
Serdar Tumgoren wrote:
> Hi everyone,
>
> I was wondering if there's anyone who can offer a use case/rationale
> for nested class?
>
In my Python Pipelines program I have:
class Count:
...
class Counter:
...
When an instance of Count is created, one or more instances of Counter
are created, belonging to that instance. There is no need for the
Counter class to be visible anywhere outside the Count class. Nesting
the definition makes that clear and puts the definition where it is easy
to find.
Also some day (down the road) there might be some other class in which
I'd like to create instances of another class named Counter. Nesting the
Counter classes makes that possible.
> For, instance, in the following example (taken from here:
> http://www.brpreiss.com/books/opus7/html/page598.html):
>
> class A(object):
>
> def __init__(self):
> self.y = 0
>
> class B(object):
>
> def __init__(self):
> self.x = 0
>
> def f(self):
> pass
>
> My initial thought was that perhaps they inherit the behavior of
> parent classes, but that apparently is not the case:
>
> Are there specific situations when nested classes come in handy
> (perhaps for grouping conceptually related classes that don't share
> attributes?).
>
> 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?
>
>
--
Bob Gailer
Chapel Hill NC
919-636-4239
More information about the Tutor
mailing list