dictionaries vs. objects
Steve Holden
steve at holdenweb.com
Mon Aug 28 04:15:20 EDT 2006
Andre Meyer wrote:
> Hi all
>
> I have the following question: I need a representation of a physically
> inspired environment. The environment contains a large number of objects
> with varying attributes. There may be classes of objects, but there is
> not necessarily a strictly defined hierarchy among them. Object access
> should be fast, because there a many objects that change often ( e.g.,
> moving objects).
>
> My approach is to use a dictionary for the environment model and store
> each object as a slot, which contains either a dictionary or an object.
> Now, which is better? What is the performance of Python to look up
> attributes as either slots of a dictionary or attributes of an object?
> What feels more logical? What is more pythonic and performs better?
> Which is constructed and modified easier and quicker? Are there effects
> that I am not yet aware of?
>
I'd suggest using objects: quite apart from the fact that name lookup in
a namespace is pretty much equivalent to a dictionary lookup (namespaces
use an internal optimised dictionary-style object) remember that objects
can have methods, and that the names are given as attributes rather than
as strings.
regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden
More information about the Python-list
mailing list