dictionaries vs. objects

Andre Meyer meyer at acm.org
Thu Aug 31 14:25:58 EDT 2006


On 8/28/06, Steve Holden <steve at holdenweb.com> wrote:
>
> 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.


>From another thread I learned that lookup in dictionaries is way faster than
accessing object attributes (which makes use of an internal dictionary).
However, the syntax is cleaner with objects and having methods and
properties is a plus. So, it seems like it is about speed vs. elegance...

regards
Andre
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20060831/0feff6a8/attachment.html>


More information about the Python-list mailing list