On Apr 11, 2:15 pm, Darren Dale <dsdal...@gmail.com> wrote: > I am working on a project that provides a high level interface to hdf5 > files by implementing a thin wrapper around h5py. I would like to > generalize the project so the same API can be used with other formats, > like netcdf or ascii files. The format specific code exists in File, > Group and Dataset classes, which I could reimplement for each format. > But there are other classes deriving from Group and Dataset which do > not contain any format-specific code, and I would like to find a way > to implement the functionality once and apply uniformly across > supported formats. This is really abstract, but I was thinking of > something along the lines of: > > format1.Group # implementation of group in format1 > format2.Group # ... > Base.DerivedGroup # base implementation of DerivedGroup, not directly > useful > format1.DerivedGroup = Base.DerivedGroup(format1.Group) # useful > format2.DerivedGroup = Base.DerivedGroup(format2.Group) # useful > > Could anyone please offer a comment, is this an appropriate use of > metaclassing, or is there maybe an easier/better alternative?
I don't fully understand metaclasses, but I think I have convinced myself that they are not what I was looking for. I think this will do what I want it to: class Group1(object): def origin(self): return "Group1" class Group2(object): def origin(self): return "Group2" def _SubGroup(superclass): class SubGroup(superclass): pass return SubGroup SubGroup = _SubGroup(Group2) sub_group = SubGroup() print sub_group.origin() -- http://mail.python.org/mailman/listinfo/python-list