<div dir="ltr">Hi Tomislav!<br><br>The behavior that you described was a bug. I fixed it and pushed the fix to the GitHub.<div><br>We didn't encounter this bug earlier because until this moment we always defined lambdas right inside the parentheses, like in the following example:<br>
</div><div><br> age = 20<br> query = Person.select(lambda p: p.age == age)</div><div><br>or </div><div><br> query = select(p for p in Persons)<br> query = query.filter(lambda p: p.age == age)<br><br>But in your example you define lambda in one namespace and then use it in another:<br>
<br> def make_lambda(age):<br> return lambda p: p.age == age<br><br> fn = make_lambda(20)<br> query = select(p for p in Persons)<br> query = query.filter(fn)<br><br>When lambda is used in such way, it takes value of 'age' outer variable not from locals or globals, but from closures.<br>
Pony ORM 0.5.1 dindn't know how to take variable values from closures. but this is fixed now, and you can take the latest version from GitHub:<br><a href="https://github.com/ponyorm/pony">https://github.com/ponyorm/pony</a><br>
<br><div>We plan to release the new version Pony ORM 0.5.2 on Monday which includes this fix, and you will be able to install it from PyPI using<br>pip install pony --upgrade<br></div><div><br>Thanks for the reporting!<br>
<br>Regards,<br>Alexander<br><br></div></div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Sun, Aug 3, 2014 at 3:36 AM, Tomislav Tustonic <span dir="ltr"><<a href="mailto:ttustonic@outlook.com" target="_blank">ttustonic@outlook.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div><div dir="ltr"><p>Hello</p><p> </p><p>I have found a workaround for the problem, like this:</p><p> </p><p>def findByAge(age):<br> fnc = "lambda p: p.age == {0}".format(age)<br> fn = eval(fnc)<br> doFindPersons(fn)</p>
<p> </p><p>But, I'd still like to know if it's possible to use lambdas directly, instead of using eval.</p><p> </p><p>Thanks, Tom</p><p> </p><div><hr>From: <a href="mailto:ttustonic@outlook.com" target="_blank">ttustonic@outlook.com</a><br>
To: <a href="mailto:ponyorm-list@ponyorm.org" target="_blank">ponyorm-list@ponyorm.org</a><br>Date: Sun, 3 Aug 2014 00:54:35 +0200<br>Subject: [PonyORM-list] Using lambda in Query filter<div><div class="h5"><br><br>
<div dir="ltr"><p>Hello</p><p> </p><p>I'm having a problem using passing lambda expressions as a parameter in Query filter method.</p><p>In the following example call to 'findByAge0' works fine, but calling 'findByAge' raises an exception:</p>
<p>pony.orm.core.ExprEvalError: age raises NameError: name 'age' is not defined</p><p> </p><p>db = Database('sqlite', ':memory:') </p><p>class Person(db.Entity):<br> name = Required(unicode)<br>
age = Required(int)<br>db.generate_mapping(create_tables=True)</p><p><br>with db_session:<br> p1 = Person(name="Bing Bang", age=31)<br> p2 = Person(name="Kick Mick", age=75)</p><p> </p><p>def findByAge0(age):<br>
fn = lambda p: p.age == age<br> q = select(p for p in Person)<br> q = q.filter(fn)<br> with db_session:<br> persons = list(q)<br> for p in persons:<br> print <a href="http://p.name" target="_blank">p.name</a></p>
<p> </p><p>def findByAge(age):<br> fn = lambda p: p.age == age<br> doFindPersons(fn)</p><p> </p><p>def doFindPersons(fn):<br> q = select(p for p in Person)<br> q = q.filter(fn)<br> with db_session:<br> persons = list(q)<br>
for p in persons:<br> print <a href="http://p.name" target="_blank">p.name</a></p><p> </p><p>findByAge0(75)<br>findByAge(75)<br>a = raw_input('[Enter]')<br></p><p> </p><p>Am I doing something wrong or it's not possible to use lambda expression as in this example?</p>
<p>Thanks in advance,</p><p>Tom</p><font color="#ffffff" face="DejaVu Sans Mono" size="1"><p> </p><font color="#ffffff" face="DejaVu Sans Mono" size="1"><p> </p><font color="#ffffff" face="DejaVu Sans Mono" size="1"><p> </p>
<p> </p></font></font></font> </div>
<br></div></div>_______________________________________________
ponyorm-list mailing list
<a href="mailto:ponyorm-list@ponyorm.org" target="_blank">ponyorm-list@ponyorm.org</a>
<a href="/ponyorm-list" target="_blank">/ponyorm-list</a></div> </div></div>
<br>_______________________________________________<br>
ponyorm-list mailing list<br>
<a href="mailto:ponyorm-list@ponyorm.org">ponyorm-list@ponyorm.org</a><br>
<a href="/ponyorm-list" target="_blank">/ponyorm-list</a><br>
<br></blockquote></div><br></div>