object attributes from a dictionary

D

Darren Dale

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.)
 
P

Peter Otten

Darren said:
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:
.... 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__:
.... def __init__(self, d):
.... self.__dict__.update(d)
....
Peter
 
J

John Lenton

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
 
D

Darren Dale

John said:
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.
 
J

John Lenton

John said:
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..... def __getattr__(self, attr):
.... return self[attr]
.... def __setattr__(self, attr, val):
.... self[attr] = val
....2

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

Darren Dale

John said:
John said:
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.

... def __getattr__(self, attr):
... return self[attr]
... def __setattr__(self, attr, val):
... self[attr] = val
...

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.
 
J

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.

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 :)
 
S

Scott David Daniels

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)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top