AtrributeDict

Дамјан Георгиевски gdamjan at gmail.com
Thu Apr 30 00:10:02 EDT 2009


>> I've needed an attribute accessible dict, so I created this.
>> Are there any obviously stupid shortcomings?
> 
> If you know the attribute names ahead of time, you might consider
> using a namedtuple instead.
> See
> http://docs.python.org/library/collections.html#collections.namedtuple

I do use it, even when I don't know the attribute names, see how:

from collections import namedtuple
def build_namespace_tuple(nsmap):
    """nsmap is a dictionary of XML namespaces (from lxml)"""
    namespaces = namedtuple('namespaces', ' '.join(nsmap.keys()))
    return namespaces(*['{%s}' % x for x in nsmap.values()])

If I didn't need the dict values wrapped in {} I could've just used:
    return namespaces(*nsmap.values())


and it's all fine, just completely immutable, which is not always 
desirable.

-- 
дамјан ( http://softver.org.mk/damjan/ )

Real men don't use backups, they post their stuff on a public ftp server 
and let the rest of the world make copies.
   -- Linus Torvalds




More information about the Python-list mailing list