object attributes from a dictionary

Discussion in 'Python' started by Darren Dale, Jul 13, 2004.

  1. Darren Dale

    Darren Dale Guest

    Is there a more direct way than this to turn a dictionary into an object?

    class pupa:
    def __init__(self,initDict,*args,**kwargs):
    [setattr(self,key,initDict[key]) for key in initDict.keys()]

    larva={'a':1,'b':2}
    moth=pupa(larva)

    (ok, so I'm dorking a bit here. I havent slept in two days.)
    Darren Dale, Jul 13, 2004
    #1
    1. Advertising

  2. Darren Dale

    Peter Otten Guest

    Darren Dale wrote:

    > Is there a more direct way than this to turn a dictionary into an object?
    >
    > class pupa:
    > def __init__(self,initDict,*args,**kwargs):
    > [setattr(self,key,initDict[key]) for key in initDict.keys()]
    >
    > larva={'a':1,'b':2}
    > moth=pupa(larva)
    >
    > (ok, so I'm dorking a bit here. I havent slept in two days.)


    The most direct way is:

    >>> class pupa:

    .... def __init__(self, d):
    .... self.__dict__ = d
    ....
    >>> larva = {"a": 1, "b": 2}
    >>> moth = pupa(larva)
    >>> moth.a

    1
    >>> moth.c = 3
    >>> larva

    {'a': 1, 'c': 3, 'b': 2}
    >>> larva["x"] = 5
    >>> moth.x

    5

    So every change in moth affects larva and vice versa. If that is too direct,
    change the initializer to update instead of rebind __dict__:

    >>> class pupa2:

    .... def __init__(self, d):
    .... self.__dict__.update(d)
    ....
    >>> moth2 = pupa2(larva)
    >>> moth2.c

    3
    >>> moth2.d = 4
    >>> larva

    {'a': 1, 'x': 5, 'c': 3, 'b': 2} # no "d" key
    >>>


    Peter
    Peter Otten, Jul 13, 2004
    #2
    1. Advertising

  3. Darren Dale

    John Lenton Guest

    On Tue, 13 Jul 2004 18:24:49 -0400, Darren Dale <> wrote:
    > Is there a more direct way than this to turn a dictionary into an object?
    >
    > class pupa:
    > def __init__(self,initDict,*args,**kwargs):
    > [setattr(self,key,initDict[key]) for key in initDict.keys()]
    >
    > larva={'a':1,'b':2}
    > moth=pupa(larva)
    >
    > (ok, so I'm dorking a bit here. I havent slept in two days.)


    class pupa(dict):
    def __getattr__(self, attr):
    return self[attr]
    def __setattr__(self, attr, val):
    self[attr] = val

    --
    John Lenton () -- Random fortune:
    bash: fortune: command not found
    John Lenton, Jul 14, 2004
    #3
  4. Darren Dale

    Darren Dale Guest

    John Lenton wrote:
    > On Tue, 13 Jul 2004 18:24:49 -0400, Darren Dale <> wrote:
    >
    >>Is there a more direct way than this to turn a dictionary into an object?
    >>
    >>class pupa:
    >> def __init__(self,initDict,*args,**kwargs):
    >> [setattr(self,key,initDict[key]) for key in initDict.keys()]
    >>
    >>larva={'a':1,'b':2}
    >>moth=pupa(larva)
    >>
    >>(ok, so I'm dorking a bit here. I havent slept in two days.)

    >
    >
    > class pupa(dict):
    > def __getattr__(self, attr):
    > return self[attr]
    > def __setattr__(self, attr, val):
    > self[attr] = val
    >


    Maybe I'm missing a step, but this doesnt work.
    Darren Dale, Jul 14, 2004
    #4
  5. Darren Dale

    John Lenton Guest

    On Wed, 14 Jul 2004 10:51:28 -0400, Darren Dale <> wrote:
    > John Lenton wrote:
    > > On Tue, 13 Jul 2004 18:24:49 -0400, Darren Dale <> wrote:
    > >
    > >>Is there a more direct way than this to turn a dictionary into an object?
    > >>
    > >>class pupa:
    > >> def __init__(self,initDict,*args,**kwargs):
    > >> [setattr(self,key,initDict[key]) for key in initDict.keys()]
    > >>
    > >>larva={'a':1,'b':2}
    > >>moth=pupa(larva)
    > >>
    > >>(ok, so I'm dorking a bit here. I havent slept in two days.)

    > >
    > >
    > > class pupa(dict):
    > > def __getattr__(self, attr):
    > > return self[attr]
    > > def __setattr__(self, attr, val):
    > > self[attr] = val
    > >

    >
    > Maybe I'm missing a step, but this doesnt work.


    works here :)

    Python 2.3.4 (#2, Jul 5 2004, 09:15:05)
    [GCC 3.3.4 (Debian 1:3.3.4-2)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> class pupa(dict):

    .... def __getattr__(self, attr):
    .... return self[attr]
    .... def __setattr__(self, attr, val):
    .... self[attr] = val
    ....
    >>> larva={'a':1,'b':2}
    >>> moth=pupa(larva)
    >>> moth.a

    1
    >>> moth.b

    2

    unless you mean something else by "this doesn't work"

    --
    John Lenton () -- Random fortune:
    bash: fortune: command not found
    John Lenton, Jul 14, 2004
    #5
  6. Darren Dale

    Darren Dale Guest

    John Lenton wrote:
    > On Wed, 14 Jul 2004 10:51:28 -0400, Darren Dale <> wrote:
    >
    >>John Lenton wrote:
    >>
    >>>On Tue, 13 Jul 2004 18:24:49 -0400, Darren Dale <> wrote:
    >>>
    >>>
    >>>>Is there a more direct way than this to turn a dictionary into an object?
    >>>>
    >>>>class pupa:
    >>>> def __init__(self,initDict,*args,**kwargs):
    >>>> [setattr(self,key,initDict[key]) for key in initDict.keys()]
    >>>>
    >>>>larva={'a':1,'b':2}
    >>>>moth=pupa(larva)
    >>>>
    >>>>(ok, so I'm dorking a bit here. I havent slept in two days.)
    >>>
    >>>
    >>> class pupa(dict):
    >>> def __getattr__(self, attr):
    >>> return self[attr]
    >>> def __setattr__(self, attr, val):
    >>> self[attr] = val
    >>>

    >>
    >>Maybe I'm missing a step, but this doesnt work.

    >
    >
    > works here :)
    >
    > Python 2.3.4 (#2, Jul 5 2004, 09:15:05)
    > [GCC 3.3.4 (Debian 1:3.3.4-2)] on linux2
    > Type "help", "copyright", "credits" or "license" for more information.
    >
    >>>>class pupa(dict):

    >
    > ... def __getattr__(self, attr):
    > ... return self[attr]
    > ... def __setattr__(self, attr, val):
    > ... self[attr] = val
    > ...
    >
    >>>>larva={'a':1,'b':2}
    >>>>moth=pupa(larva)
    >>>>moth.a

    >
    > 1
    >
    >>>>moth.b

    >
    > 2
    >
    > unless you mean something else by "this doesn't work"
    >

    I see. I wasnt clear when I asked my original question. A dictionary is
    already an object. I wanted to take a dictionary and by creating a new
    object, turn the key/value pairs into object attributes.

    Using Peters class definition,
    larva={'a':1,'b':2}
    moth=pupa(larva)
    vars(moth) --> a dictionary listing attribute/value pairs.

    Using your definition, John, vars(moth) yields an empty dictionary.
    Thats why I didnt think it was working, but you are right. It works,
    just not the way I looking for.
    Darren Dale, Jul 14, 2004
    #6
  7. Darren Dale

    John Lenton Guest

    On Wed, 14 Jul 2004 11:39:07 -0400, Darren Dale <> wrote:
    >
    > I see. I wasnt clear when I asked my original question. A dictionary is
    > already an object. I wanted to take a dictionary and by creating a new
    > object, turn the key/value pairs into object attributes.
    >
    > Using Peters class definition,
    > larva={'a':1,'b':2}
    > moth=pupa(larva)
    > vars(moth) --> a dictionary listing attribute/value pairs.
    >
    > Using your definition, John, vars(moth) yields an empty dictionary.
    > Thats why I didnt think it was working, but you are right. It works,
    > just not the way I looking for.


    you hadn't mentioned vars :)

    --
    John Lenton () -- Random fortune:
    bash: fortune: command not found
    John Lenton, Jul 14, 2004
    #7
  8. class pupa:
    def __init__(self, **kwargs):
    self.__dict__.update(kwargs)




    Darren Dale <> wrote in message news:<cd1nfj$15$>...
    > Is there a more direct way than this to turn a dictionary into an object?
    >
    > class pupa:
    > def __init__(self,initDict,*args,**kwargs):
    > [setattr(self,key,initDict[key]) for key in initDict.keys()]
    >
    > larva={'a':1,'b':2}
    > moth=pupa(larva)
    >
    > (ok, so I'm dorking a bit here. I havent slept in two days.)
    Lonnie Princehouse, Jul 14, 2004
    #8
  9. Darren Dale wrote (in response to John Lenton):

    > I see. I wasnt clear when I asked my original question. A dictionary is
    > already an object. I wanted to take a dictionary and by creating a new
    > object, turn the key/value pairs into object attributes.
    >


    Is this what you were looking for?:
    ...
    def __init__(self, initDict, ...):
    for key, val in initDict.iteritems():
    setattr(self, key, val)

    or were you looking to explicitly play with magic?:

    ... # probably only on classic classes:
    def __init__(self, initDict, ...):
    self.__dict__.update(initDict)

    --
    -Scott David Daniels
    Scott David Daniels, Jul 15, 2004
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ilias Lazaridis
    Replies:
    6
    Views:
    427
    Ilias Lazaridis
    Feb 21, 2006
  2. Replies:
    17
    Views:
    556
    Antoon Pardon
    Aug 29, 2006
  3. Peter Otten
    Replies:
    1
    Views:
    294
    Fredrik Lundh
    Aug 25, 2006
  4. james_027
    Replies:
    1
    Views:
    307
    Marc 'BlackJack' Rintsch
    Aug 22, 2007
  5. Navkirat Singh
    Replies:
    6
    Views:
    2,967
    Navkirat Singh
    Jul 29, 2010
Loading...

Share This Page