design question, metaclasses?

Darren Dale dsdale24 at gmail.com
Sun Apr 12 15:52:44 EDT 2009


On Apr 12, 3:23 pm, Aaron Brady <castiro... at gmail.com> wrote:
> On Apr 12, 1:30 pm, Darren Dale <dsdal... at gmail.com> wrote:
>
>
>
> > On Apr 11, 2:15 pm, Darren Dale <dsdal... at gmail.com> wrote:
>
> _
>
> > > 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
>
> _
>
> > 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()
>
> You can create new types in one statement:
>
> SubGroup= type( "SubGroup", ( BaseGroup, ), { } )

But how can I implement the *instance* behavior of SubGroup with this
example? In my original example:

format1.Group # implementation of group in format1
format2.Group # implementation of group in format2
Base.DerivedGroup # base implementation of DerivedGroup, must subclass
a group
format1.DerivedGroup = Base.DerivedGroup(format1.Group) # useful
format2.DerivedGroup = Base.DerivedGroup(format2.Group) # useful

I'm trying to achieve uniform behavior of my derived groups across
supported formats. My derived groups are abstracted such that they do
not need to be reimplemented for each format, I only need to implement
Group for each format. This is a real mind bender for me, even my
factory function gets hairy because I have additional classes that
derive from DerivedGroup. Maybe what I need is the ability to provide
context at import time, is that possible?



More information about the Python-list mailing list