Differences between Class(Object) and Class(Dict) for dictionary usage?

Random832 random832 at fastmail.com
Tue Apr 26 23:56:00 EDT 2016


On Tue, Apr 26, 2016, at 23:43, Christopher Reimer wrote:
> Greetings,
> 
> If I'm using a dictionary to store variables for an object, and 
> accessing the variable values from dictionary via property decorators, 

what exactly do you mean by property decorators? If you're just
accessing them in a dictionary what's the benefit over having the values
be simple attributes rather than properties?

> would it be better to derive the class from object or dict?
> 
>      class Test1(object):
>          def __init__(self):
>              self.state = {'key': 'value'}
> 
> Or:
> 
>      class Test2(dict):
>          def __init__(self):
>                  self.__dict__ = {'key', 'value'}
> 
> I haven't seen a good pro/con discussion on the Internet for using one 
> over the other. I played with both in my code. Doesn't seem to make a 
> great difference either way. Using object seems to be the most simplest 
> approach.

I sometimes use dict (with self.__dict__ = self) if I want to be able to
access values via either obj['key'] or obj.key (a la Javascript).
Otherwise, there's no point in using dict.



More information about the Python-list mailing list