On Wed, Aug 24, 2016 at 2:55 AM, Jinghui Niu <niujing...@gmail.com> wrote:
> Hi, I wonder if there is any recommendation or best practice on choosing
> between
> hybrid_property
>
> and
> hybrid_method
> ,
> other than they hybrid_method can take arguments? If I use the hybrid_method
> only throughout, without giving it a argument more than self, doesn't it
> equal to using hybrid_property? Thanks.

It's the same trade-off as using normal python properties vs methods.
For properties, the underlying code is executed when the attribute is
accessed, whereas methods are executed when they are *called*. For
example, if you had this:

class Interval(Base):
    __tablename__ = 'interval'

    id = Column(Integer, primary_key=True)
    start = Column(Integer, nullable=False)
    end = Column(Integer, nullable=False)

    def __init__(self, start, end):
        self.start = start
        self.end = end

    @hybrid_property
    def length_property(self):
        return self.end - self.start

    @hybrid_method
    def length_method(self):
        return self.end - self.start

...then you could query using the property like this:

    session.query(Interval).filter(Interval.length_property > 5)

But the method would have to be used like this:

    session.query(Interval).filter(Interval.length_method() > 5)

It's up to you which you prefer.

Hope that helps,

Simon

-- 
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.

Reply via email to