Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #107705
| From | Random832 <random832@fastmail.com> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: Differences between Class(Object) and Class(Dict) for dictionary usage? |
| Date | 2016-04-26 23:56 -0400 |
| Message-ID | <mailman.140.1461729362.32212.python-list@python.org> (permalink) |
| References | <5720357B.4060009@icloud.com> <1461729360.3276080.590752673.7A6E1041@webmail.messagingengine.com> |
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.
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Differences between Class(Object) and Class(Dict) for dictionary usage? Random832 <random832@fastmail.com> - 2016-04-26 23:56 -0400
csiph-web