Subclassing by monkey-patching

Glazner yoavglazner at gmail.com
Mon Sep 6 12:40:23 CEST 2010


On Sep 6, 11:08 am, Bruno Desthuilliers <bruno.
42.desthuilli... at websiteburo.invalid> wrote:
> Jason a écrit :
>
> > On Sep 5, 3:53 pm, Peter Otten <__pete... at web.de> wrote:
> >>>>> m = gio.File(".").monitor_directory()
> >>>>> C = type(m)
>
> > 'C' will not necessarily be 'gio.FileMonitor' — I think the internals
> > of the GIO methods might further "subclass" it in some way depending
> > on what underlying monitors are available.
>
> >> A possible alternative may be a class that wraps a FileMonitor instead of
> >> subclassing it.
>
> > I've been avoiding this because it involves a lot of boilerplate: the
> > signals needs to be replicated and passed through, same for all
> > GObject properties, same for the usual methods.
>
> Python is not Java !-)http://docs.python.org/reference/datamodel.html#object.__getattr__

Yep, thats what i do...

def MySubclass(object):
  def __init__(self):
    self._ins = SomeObject()
  def __getattr__(self,name):
    return getattr(self._ins,name)



More information about the Python-list mailing list