[Tutor] Class within class, or...?

EJP ejp at zomething.com
Wed Jan 26 06:02:34 CET 2005


[sent previously, please ignore if already rec'd]


A basic OOP question, I guess.

I have a file from which I want to extract some data.  The data of
interest may be towards the end of the file at position A, defined by
schema A; or it may be in the file at variable positions near the
beginning of the file which are defined by a combination of schema B
and the data in the file; or the data of interest may be in the file
both ways (and the data may be different).

I'd like to write a class for the data extraction.  In my mind I'd
have one overall class for the data extraction with attributes for the
two possible sets of data extracted.  I am thinking It'd be nice if
these two attributes were two different classes themselves.  Something
like this:

class Reader:
   def __init__(self, filePath=""):
       try:
           self.fileObj=file(filePath,"r")
       except:
           self.errorMsg="File opening error"
       self.dataA=SchemaA()
       self.dataB=SchemaB()
       ...

class SchemaA():
   def __init__(self):
       self.data={}
       ...

class SchemaB():
   def __init__(self):
       self.data={}
       ...

I'm struggling with a mind stuck in functional programming mode and
also lazy from simple scripting, thus my questions:

-  Is there any problem with structuring my classes like this?  I
assume that using SchemaA and SchemaB within Reader would be no
different than using any other classes within that class (it all gets sorted out in compilation).

-  Is it possible, and is there any advantage to, embedding the
classes (SchemaA and SchemaB) within the Reader class?  The Schema
classes would not be used elsewhere.

-  Are there performance considerations to the structure I use?

Thanks in advance,

Eric




More information about the Tutor mailing list