Creating object attributes

Larry Bates lbates at
Thu Jul 22 23:55:13 CEST 2004

I think we have all done something like what you have done.

I have the class create the attributes dynamically.
Something like (not tested).  I'm leaving the fixed record
parsing to you.

class FixedLengthRecord:
    def __init__(self, fields):
        for field in fields:

    def __getitem__(self, key):
        try: return self.__dict__[key]
            print "Field name '%s' not found in current record" % key
            return None

    def __setitem__(self, key, value):
        if self.__dict__.has_key(key): self.__dict__[key]=value
            print "Field name '%s' not found in current record" % key

    def __call__(self, key):
        try: return self.__dict__[key]
            print "Field name '%s' not found in current record" % key
            return None

    def append(self, field, value=None):
        if not self.__dict__.has_key(field): self.__dict__[field]=value
            print "Field name '%s' already found in current record" % key

Then you can do:

myFields = ('first_name', 'last_name')

then these statements work

record.address="123 Morning Glory Lane"


print record('first_name') outputs in "Greg"

Eliminates need for .set and .get methods and grows fieldnames

I hope this is what you were looking for.

Larry Bates
Syscon, Inc.

"Greg Lindstrom" <greg.lindstrom at> wrote in message
news:mailman.726.1090528236.5135.python-list at
> Hello-
> I have created a class,, that allows me to manipulate
> fixed length records in the routines I write.  I recently converted it to
> read record layouts from an SQL server (it used to read config files) and
> am thinking of making another change to make my code cleaner [WARNING: I
> going to be violating "pure" OO theology...if that offends you, please
> now].
> The heart of the class is a dictionary named "fields" that stores the
> current value, length, data type, and default value for the field.  Each
> of the dictionary corresponds to a "logical name" read in from the
> I then have "Get()" and "Set()" methods to -- as you might have guessed --
> get and set the values.  Other methods are Clear(), Spreadsheet(),
> Serialize(), Unserialize(), etc.  So, in my application code I might have
> something like the following:
> myRec = FixedLengthRecord( id='gsl0001', version='1.0', description='This
> my record!')
> myRec.Set('first_name', 'Greg')          # only field names in the record
> layout may be set this way
> myRec.Set('last_name', 'Lindstrom')
> giver = myRec.Get('first_name')
> get the idea.  What I would like to do, or at least consider, is
> adding an attribute for each data field value instead of adding it to the
> dictionary so I could access my data as follows:
> giver = myRec.first_name
> I still would use the Set() methods because they insure the fields are
> than or equal to the maximum length allowed.  This violates the OO
> of accessor methods, but it cleans up my application code and, since I
> on reality street and not academia, I am interested in how to do it.
> So, to simplify the project a tad, suppose I had a tuple of fields.
> myFields = ('first_name', 'last_name')
> How could I incorporate them into an class "on the fly" to produce the
> equivalent of
> self.first_name = 'None
> self.last_name = None
> Are there other ways to handle fixed length records?
> Thanks!
> --greg
> Greg Lindstrom                                         (501) 975-4859
> NovaSys Health                  greg.lindstrom at
> "We are the music makers, and we are the dreamers of dreams"  W.W.

More information about the Python-list mailing list