all() is slow?

Steven D'Aprano steve+comp.lang.python at pearwood.info
Thu Nov 10 17:56:41 EST 2011


On Thu, 10 Nov 2011 14:19:18 -0700, Ian Kelly wrote:

> On Thu, Nov 10, 2011 at 1:37 PM, Devin Jeanpierre
> <jeanpierreda at gmail.com> wrote:
>> Of course not. I do, however, think that it's conceivable that I'd want
>> to key a namedtuple by an invalid identifier, and to do that, yes, I'd
>> need to use getattr().
> 
> Care to give a real use case?  

A common use-case is for accessing fields from an external data source, 
using the same field names. For example, you might have a database with a 
field called "class", or a CSV file with columns "0-10", "11-20", etc.

Personally, I wouldn't bother using attributes to access fields, I'd use 
a dict, but some people think it's important to use attribute access.


> You could even go a step further and use,
> say, arbitrary ints as names if you're willing to give up getattr() and
> use "ob.__class__.__dict__[42].__get__(ob, ob.__class__)" everywhere
> instead.  The fact that somebody might conceivably want to do this
> doesn't make it a good idea, though.

Obviously you would write a helper function rather than repeat that mess 
in-line everywhere.


-- 
Steven



More information about the Python-list mailing list