> -----Original Message----- > From: [email protected] > [mailto:[EMAIL PROTECTED] On Behalf Of klaus > Sent: 22 October 2007 11:33 > To: sqlalchemy > Subject: [sqlalchemy] Please add some __len__ methods > > > Hi all, > I wonder why some classes/objects implement part of a list interface - > but without a __len__ method. Obvious examples are: > > Query has __iter__ and __getitem__, both of which access the database. > __len__ would be a nice alternative to a basic count(). > > Session has __iter__ and __contains__. __len__ could be used to > indicate when the session gets too large and should be cleared. > > I already suggested this some months ago but attracted no > attention. ;-) > > Best regards > Klaus >
As far as Query is concerned, SQLObject used to have this feature, but they removed it because it was too easy for it to be called implicitly: http://www.sqlobject.org/News.html#id31 For example: >>> class a(object): ... def __len__(self): ... print "__len__" ... return 5 ... def __iter__(self): ... print "__iter__" ... return iter([1, 2, 3, 4, 5]) ... >>> >>> b = a() >>> list(b) __iter__ __len__ [1, 2, 3, 4, 5] >>> So converting a Query to a list would generate an unnecessary COUNT query. (I'm not sure why __iter__ gets called before __len__ - I would have guessed it would be the other way round) I can't think of an argument for not having __len__ on Session though. Simon --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~----------~----~----~----~------~----~------~--~---
