On 8 Dec 2012, at 12:20 PM, Lewis <[email protected]> wrote: > Which syntax is preferred, and if so, why? > > syntax 1: rows[0]['auth_user.first_name'] > syntax 2: rows.first().auth_user.first_name
Partial answer: rows.first() is a convenience that defers the need to check whether rows is empty. It's essentially: rows[0] if rows else None Either syntax will raise an exception if rows is empty, which means you need to guarantee that rows is not empty beforehand, and there's no advantage to using rows.first(). For the rest of it, do you know that both syntaxes work as expected? > > 1 seems closer to common Python syntax and is likely more interpretable to > any Python programmer. 2 seems a bit more readable but relies on knowing > the DAL methods and seems a bit ad hoc. > > Is this a third: rows[0].['auth_user']['first_name']? This would seem the > most consistent with Python dictionary referencing syntax. > > And I could have added .as_list() to my initial query and a;sp used the third > syntax. This is of course doing something different by serializing the row > into a real Python dictionary and losing the db affinity. > > In general, web2py seems to have a few too many alternative ways to do the > same thing, which are a bit of syntactic sugar or ways to save a few > characters of typing. Neither seem like good reasons... Multiple ways to do > the same thing (when, indeed it is the "same") seems confusing, especially if > some of the variants only work in certain cases. Best to have one way that > always works... Of course, I am free to use the "basic" way and others can > use the variants. > > Web2py is working great and I ask only to clean up my code (most of the > problems are very much mine!). > --

