On Fri, Aug 4, 2017 at 6:07 PM, Shane Carey <shanecare...@gmail.com> wrote: > I am trying to remove duplicate code in this scenario described here > > http://docs.sqlalchemy.org/en/latest/orm/constructors.html > > class Obj(Base): > def __init__(self, **kwargs): > super().__init__(**kwargs) > self.extra = # ... some complicated construction based off of a > kwarg > > @orm.reconstructor > def init_on_load(self): > self.extra = # ... same complicated construction based off of self > attribute
normally you'd factor the complexity into a new method and have __init__ and init_on_load call that: class Obj(Base): def __init__(self, **kw): self._do_complex_init(kw['foo'], kw['bar']) @orm.reconstructor def init_on_load(Self): self._do_complex_init(self.foo, self.bar) > Is it possible to intercept the setting of the attribute self.extra depends > on What kind of attribute (ORM mapped column? plain attribute not linked to a SQL expression?) and then when you say "setting" do you mean userland setting (like you said myobj.the_attribute = 5) or you mean when it is loaded by the ORM? Overall I think you might be interested in these events: init - http://docs.sqlalchemy.org/en/latest/orm/events.html?highlight=mapperevents#sqlalchemy.orm.events.InstanceEvents.init load - http://docs.sqlalchemy.org/en/latest/orm/events.html?highlight=mapperevents#sqlalchemy.orm.events.InstanceEvents.load Above, the "load" event is what is used to implement @orm.reconstructor which is from before we had a fully consistent event system. I'm pushing out a change in the documentation that correctly links @orm.reconstructor to the InstanceEvents.load event. -- SQLAlchemy - The Python SQL Toolkit and Object Relational Mapper http://www.sqlalchemy.org/ To post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See http://stackoverflow.com/help/mcve for a full description. --- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at https://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.