A class for C-like structuures in Python

Thomas Gagne tgagne at ix.netcom.com
Wed Aug 9 12:15:28 EDT 2000


Hmm... Now I get the following instantiating the structure:

Traceback (innermost last):
  File ".//isdclient.py", line 4, in ?
    h = IsdHeader()
  File "./IsdIO.py", line 7, in __init__
    CStructure.__init__(self, (
  File "./CStructure.py", line 11, in __init__
    self.fmt = self.fmt + f
  File "./CStructure.py", line 22, in __setattr__
    self.__checkname(name)
  File "./CStructure.py", line 15, in __checkname
    raise AttributeError, "No struct-field is named " + name
AttributeError: No struct-field is named fmt

I think there's a problem mixing __dict__ with standard variable definitions.
Whether I use __dict__['fmt'] = '' or the simpler self.fmt = '' I get the same
result (above)

tgagne:/home/tgagne/work/isect/python head -20 CStructure.py
import struct

class CStructure:
 def __init__(self, theTuples):
  self.__dict__['members'] = {}
  self.__dict__['fmt'] = ''
  self.__dict__['shape'] = theTuples

  for n,f in self.shape:
   self.members[n] = None
   self.fmt = self.fmt + f

 def __checkname(self, name):
  if not self.members.has_key(name):
   raise AttributeError, "No struct-field is named " + name
 def __getattr__(self, name):
  self.__checkname(name)
  return self.members[name]




More information about the Python-list mailing list