is attribute access a hash-lookup by name?

Aahz aahz at pythoncraft.com
Wed Jul 3 12:37:04 EDT 2002


In article <66715c02.0207021737.b488f07 at posting.google.com>,
Steven Shaw <steven_shaw at adc.com> wrote:
>
>As I understand it, in Python, an object is an associative array or
>hash. 

Not quite true.  It's more accurate to say that an object is a chunk of
memory.  However, the vast majority of objects contain a dict to perform
attribute lookup.

>I imagine this means that every attribute access is a
>hash-lookup by name. Does the implementation optimise this away
>somehow?

Not usually, except for some built-in objects and function local names.

>Is there a way to have structs/records in Python where the members of
>the struct are looked up by offset (not by name)?

Tuple or list would be the obvious ones, though it's possible in 2.2 to
have fancy two-way structures that can do both offset-based and
name-based lookups (see os.stat for an example).  Also, with new-style
classes, you can use __slots__ to force vectorized lookups, but it's
more a memory-saving technique than a speedup.
-- 
Aahz (aahz at pythoncraft.com)           <*>         http://www.pythoncraft.com/

Project Vote Smart: http://www.vote-smart.org/



More information about the Python-list mailing list