speed wise, this is better: hasattr is implemented as getattr + try except. i would do it even: f = getattr(instance, "_pre_insert", None) if f: f() Thus the func name is spelled only once - avoids stupid mistakes.
On Thursday 15 May 2008 17:36:52 Roger Demetrescu wrote: > On 5/15/08, Yannick Gingras <[EMAIL PROTECTED]> wrote: > > Michael Bayer <[EMAIL PROTECTED]> writes: > > > easy enough to build yourself a generic MapperExtension that > > > scans incoming objects for a "_pre_commit()" method. > > > > Yeah indeed. I used this: > > > > ------------------------------ > > class HookExtension(MapperExtension): > > """ Extention to add pre-commit hooks. > > > > Hooks will be called in Mapped classes if they define any of > > these methods: > > * _pre_insert() > > * _pre_delete() > > * _pre_update() > > """ > > def before_insert(self, mapper, connection, instance): > > if getattr(instance, "_pre_insert", None): > > instance._pre_insert() > > return EXT_CONTINUE > > [cut] > > Any reason for not using hasattr ? Like... > > def before_insert(self, mapper, connection, instance): > if hasattr(instance, "_pre_insert"): > instance._pre_insert() > return EXT_CONTINUE > > > Cheers, > > Roger > > --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To post to this group, send email to sqlalchemy@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~----------~----~----~----~------~----~------~--~---