Here's my general-purpose solution for doing this:

class Dict2Class(object):
    """
    Update like a dictionary, but expose the keys as class properties.
Sweet!
    You can instantiate and update this practically any way you
choose, and
    the values are available as class properties.
    >>> c = Dict2Class((('fred', 11), ('joe', 88)), bob=9)
    >>> c.bob
    9
    >>> c.joe
    88

    >>> c = Dict2Class({'bob': 88, 'fred': 9})
    >>> c.fred
    9

    >>> c = Dict2Class()
    >>> c.bob = 88
    >>> c.bob
    88

    This subclasses plain old object. It could also subclass dict to
provide
    even more functionality, but at the risk of naming collisions
between
    the dict methods and property names.
    """
    def __init__(self, *e, **f):
        self.__dict__ = dict(*e, **f)
    def update(self, *e, **f):
        self.__dict__.update(*e, **f)
    # Looks a little complex, but it rocks.



On Jun 19, 2:17 pm, Amita Ekbote <amita.ekb...@gmail.com> wrote:
>  Hello,
>
> I am retrieving values from a database in the form of a dictionary so
> I can access the values as d['column'] and I was wondering if there is
> a way to convert the hash to a struct like format so i can just say
> d.column. Makes it easier to read and understand.
>
> Thanks
> Amita
>
> --
> Amita Ekbote

-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to