having a function called after the constructor/__init__ is done

alex goretoy aleksandr.goretoy at gmail.com
Mon Mar 16 23:42:34 EDT 2009


__del__

I use it to close a mysql connection

-Alex Goretoy
http://www.goretoy.com

E. B. White  - "Be obscure clearly."

On Mon, Mar 16, 2009 at 10:11 PM, thomas.hansen at gmail.com <
thomas.hansen at gmail.com> wrote:

> Hi all,
>
> We've been breaking our heads over a good way of accomplishing an
> "on_load" event in our multitouch GUI frameowork PyMT.  We think we'd
> like to trigger an "on_load" event after a class is fully instantiated
> (or just a function call for simplicity, so you dont need to worry
> about our specific system).  This is so that if I make a subclass of
> some widget, I dont have to overwrite the __init__ method (use super,
> know the args, and pass them) everytime.  Maybe I just want to do some
> really basic stuff that doesn't really have anything to do with the
> parent class.
>
> Anyway.  My first attempt was to simply put a call to self.on_load at
> the end of the widget base class.  This doesn't work though, because,
> if I subclass it and do things after the call to super.__init__,
> on_load will be called before the constructor finishes (when the top
> most parent class __init__ finishes)
>
> We've sort of managed to achieve this by using a decorator that
> changes the __init__ function.  but even this doesnt seem like the
> best way to do this.  a) I now have to decorate every constructor i
> ever write. b) it seems impossible to make it so that it only happens
> once after the object is actually instantiated (since i have a
> decorator on all my constructors..each constructor in the inheritance
> line will call on_load once)
>
> Can I do something fancy with metaclasses here?  I think I could kind
> of do this by making all my classes use __new__, and then essentially
> use __init__ as what i want on_load to be...but that just seems really
> nasty and unpythonic.  not to speak about the confusion it would cause
> with people trying to figure out the library.
>
> So any ideas on how to get a function called on an object just after
> __init__ is done executing?
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20090316/d3242c7e/attachment-0001.html>


More information about the Python-list mailing list