design question, metaclasses?

Piet van Oostrum piet at cs.uu.nl
Tue Apr 14 14:31:19 CEST 2009


>>>>> Darren Dale <dsdale24 at gmail.com> (DD) wrote:

>DD> On Apr 11, 2:15 pm, Darren Dale <dsdal... at 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?

>DD> I don't fully understand metaclasses, but I think I have convinced
>DD> myself that they are not what I was looking for. I think this will do
>DD> what I want it to:

>DD> class Group1(object):

>DD>     def origin(self):
>DD>         return "Group1"


>DD> class Group2(object):

>DD>     def origin(self):
>DD>         return "Group2"


>DD> def _SubGroup(superclass):

>DD>     class SubGroup(superclass):
>DD>         pass

>DD>     return SubGroup


>DD> SubGroup = _SubGroup(Group2)

What is the difference of this with:

class SubGroup(Group2):
      pass
?
>DD> sub_group = SubGroup()

>DD> print sub_group.origin()

>From your description I find it very difficult to understand what you
want. But I have some feeling that multiple inheritance may be something
you could use.
-- 
Piet van Oostrum <piet at cs.uu.nl>
URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
Private email: piet at vanoostrum.org



More information about the Python-list mailing list