[Python-ideas] Storages Re: Idea for the mapping API

Roman Susi rnd at onego.ru
Sun Feb 10 19:31:30 CET 2008


Raymond Hettinger wrote:
> Perhaps, the dict() constructor should accept any object with a __dict__ method.
> 
> The principle is that almost any object that has key and value pairs (named tuples for example) should be readily convertable to and 
> from a dictionary.  The current, non-uniform alternative is for the object to provide an asdict() method and for the user to call it 
> directly.
> 
> Raymond

BTW, will the coming ABC have classes with "setattr ~= setitem"
functionality? Yes, its quite easy to do (docstring from web.py
utils.Storage is not longer than implementation):

    """
    A Storage object is like a dictionary except `obj.foo` can be used
    in addition to `obj['foo']`.

        >>> o = storage(a=1)
        >>> o.a
        1
        >>> o['a']
        1
        >>> o.a = 2
        >>> o['a']
        2
        >>> del o.a
        >>> o.a
        Traceback (most recent call last):
            ...
        AttributeError: 'a'

    """

At least, this could set the bare protocol of such hybrid for anyone to
subclass.


-Roman



More information about the Python-ideas mailing list