Differences between Class(Object) and Class(Dict) for dictionary usage?
Ethan Furman
ethan at stoneleaf.us
Wed Apr 27 01:31:18 EDT 2016
On 04/26/2016 08:43 PM, Christopher Reimer wrote:
> If I'm using a dictionary to store variables for an object, and
> accessing the variable values from dictionary via property decorators,
> 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.
Using a dict gets you a bunch of methods for free: keys(), values(),
items(), get(), etc., etc..
If you don't need any of those, subclass object.
--
~Ethan~
More information about the Python-list
mailing list