On Thu, Sep 29, 2016 at 6:32 AM, Jinghui Niu <[email protected]> wrote:
> The documentation shows that hybrid_property should used as a decorator,
> like:
> @hybrid_property
> def my_property(self):
> pass
>
>
> What if I wanted to give this hybrid property a name by referring a variable
> in runtime? Is this allowed? Thanks.
>
I'm not entirely sure what you mean - I can't imagine a situation
where this would be useful. But you ought to be able to add hybrid
properties to a class after the class has been defined, and you can
use setattr if the name is stored in a variable. So I imagine
something like this ought to work:
def fget(self):
# code when property is accessed on instance
pass
def fset(self, value):
# code when property is assigned to
pass
def expr(cls):
# code when property is accessed on class
pass
prop = hybrid_property(fget, fset=fset, expr=expr)
# Add the hybrid property to a class under the name "propertyname"
setattr(SomeClass, 'propertyname', prop)
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 [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.