global event listeners

Josiah Carlson jcarlson at uci.edu
Wed Nov 17 14:06:13 EST 2004


"Hunter  Peress" <hunterp at gmail.com> wrote:
> 
> 
> Josiah Carlson wrote:
> > "Hunter  Peress" <hunterp at gmail.com> wrote:
> > >
> > > i suppose i could do this, and have an "if self.__name__ ==
> > > what_im_looking_for and type(self) == what_im_also_looking_for"
> > >
> > > next is how can i make every object in a given runtime inherit from
> > > this class
> 
> >
> > class counter(object):
> >     counter = 0
> >     def __init__(self, value):
> >         self.__value = value
> >
> >     def get_v(self):
> >         return self.__value
> >     def set_v(self, val):
> >         self.__value = val
> >         counter.counter += 1
> >     value = property(get_v, set_v)
> >
> > >>> a = counter(1)
> > >>> b = counter(2)
> > >>> a.counter
> > 0
> > >>> b.counter
> > 0
> > >>> a.value = 0
> > >>> a.counter
> > 1
> > >>> b.counter
> > 1
> > >>>
> >
> >
> >  - Josiah
> 
> Well yes, but im looking for a non-1:1 way of making every object be a
> child of this new class.  i would have to do an insane amount of
> regex.... ;-)

Not really.  You could (as another poster suggests) make an object with
some number of dynamically created attributes, and call such an object
something like 'M' (for magic)...

M.name
M.name2
etc.

An interesting feature request would be:
Allow properties to be assigned to modules after module instantiation,
coupled with the ability to get the current module.

With those two features, then what you want is possible, though those
two features /aren't/ going to make it into Python 2.4, it it would take
quite a bit of work to make it happen in Python 2.5.


 - Josiah




More information about the Python-list mailing list