[Tutor] rationale for nested classes?

Serdar Tumgoren zstumgoren at gmail.com
Mon Aug 17 19:36:24 CEST 2009


> I would use a nested class to create a particular data structure which
> represent a "sub-unit" of the mother class. ... In this
> 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").

This is an interesting idea that I'd like to hear more about and see examples.

As part of a current project, this might in fact come in handy.
Specifically, would the above approach be a recommended way of
performing some additional data integrity checks on an object after
having created it?

Say that I've created a series of Campaign Committee objects from an
initial data set.

class Committee(object):
    def __init__(self, data):
        self.id = data[0]
        self.name = data[1]
        self.candidate = data[2]
        self.candidateID = data[3]

In the above example, I know for certain that the returned data will
always have a committee ID. But a number of other data such as the
name, candidate, and candidateID are often missing or incorrect. So
after the initialiization of an object, I'm using "self.id" in a
series of subsequent database queries to correct and/or fill in this
data.

So my question, in light of Mac's suggestion,  is this a case where I
should add a subclass containing so-called DataIntegrity checks? For
instance, something that resembles this:

class Committee(object):
    <<snip>>

    class DataFixer(self):
        def fix_name(self): pass
        def fix_candidate(self):pass
        def fix_candidateID(self):pass

Or would it be better to make this a separate class entirely, outside
of the Committee object?


More information about the Tutor mailing list