Does Python provide "Struct" data structure?

Brian Blais bblais at bryant.edu
Sat Sep 23 03:39:41 CEST 2006


Daniel Mark wrote:
> 
> 1> Does Python provide such Struct in this standard libary.
> Python has "4.3 struct -- Interpret strings as packed binary data", but
> it looks like different
> from what I really want to get.

I like the following version:

class Struct(dict):

     def __getattr__(self,name):

         try:
             val=self[name]
         except KeyError:
             val=super(Struct,self).getattr(self,name)

         return val

     def __setattr__(self,name,val):

         l=dir(self)

         if name in self:
             super(Struct,self).setattr(self,name,val)
         else:
             self[name]=val


it has the added benefit of having all of the dict methods too.


				bb

-- 
-----------------

              bblais at bryant.edu
              http://web.bryant.edu/~bblais



More information about the Python-list mailing list