Interesting approach to use lambdas. Since lambdas don't do side- effects, I checked out my virtualfields to see if my uses have side effects.
In my app I have: 12 methods total across 3 database tables 10 of those methods have no side-effects 2 have side-effects The two methods with side-effects are: 1. row.delete() ... deletes the relevant rows in related tables 2. row.toggle_x() ... toggles the boolean field of x and updates cached data on other tables and sends email notifications Perhaps this information is useful in designing the next lazy virtualfields. On Aug 25, 9:14 pm, Massimo Di Pierro <massimo.dipie...@gmail.com> wrote: > We are moving away from this because of many problems. Try this > instead. It is still experimental but may go into stable soon. > > def vfields(): > db.define_table('item', > Field('unit_price','double'), > Field('quantity','integer')) > db(db.item.id>0).delete() > > db.item.lazy_total_price=Field.lazy(lambda > self:self.item.unit_price*self.item.quantity) > > db.item.bulk_insert([{'unit_price':12.00, 'quantity': 15}, > {'unit_price':10.00, 'quantity': 99}, > {'unit_price':120.00, 'quantity': 2},]) > res = [] > for r in db(db.item.id>0).select(): > res.append([r.unit_price, r.quantity, r.lazy_total_price()]) > return dict(res=res) > > On Aug 25, 7:50 am, Martin Weissenboeck <mweis...@gmail.com> wrote: > > > > > > > > > I wanted to learn more about lazy virtual fields and therefore I have > > repeated the example from the book: > > > def vfields(): > > db.define_table('item', > > Field('unit_price','double'), > > Field('quantity','integer')) > > > db(db.item.id>0).delete() > > > class MyVirtualFields: > > def lazy_total_price(self): > > return lambda self=self: self.item.unit_price*self.item.quantity > > > db.item.virtualfields.append (MyVirtualFields()) > > > db.item.bulk_insert([{'unit_price':12.00, 'quantity': 15}, > > {'unit_price':10.00, 'quantity': 99}, > > {'unit_price':120.00, 'quantity': 2},]) > > > res = [] > > for r in db(db.item.id>0).select(): > > res.append([r.unit_price, r.quantity, r.lazy_total_price()]) > > return dict(res=res) > > > The expected output is: > > [[12.0, 15, 180.0], [10.0, 99, 990.0], [120.0, 2, 240.0]] > > > But I got > > * [[12.0, 15, *240.0]*, [10.0, 99, *240.0*], [120.0, 2, 240.0]]* > > * > > * > > *Three times the same result. > > * > > I have read the book and my program over and over again - but I cannot see > > any error.* > > * > > > Does somebody have an idea? > > Martin