Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #107724
| Path | csiph.com!feeder.erje.net!1.eu.feeder.erje.net!newsfeed0.kamp.net!newsfeed.kamp.net!fu-berlin.de!uni-berlin.de!not-for-mail |
|---|---|
| From | Ian Kelly <ian.g.kelly@gmail.com> |
| Newsgroups | comp.lang.python |
| Subject | Re: Differences between Class(Object) and Class(Dict) for dictionary usage? |
| Date | Wed, 27 Apr 2016 08:24:02 -0600 |
| Lines | 40 |
| Message-ID | <mailman.148.1461767085.32212.python-list@python.org> (permalink) |
| References | <5720357B.4060009@icloud.com> <57204EA6.7060007@stoneleaf.us> <CALwzidnr_O3m3=7RGUD1pu0Op93pZSLXKOMjHbecKRBeYn_30A@mail.gmail.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=UTF-8 |
| X-Trace | news.uni-berlin.de O3EJ9OHLpyktE3z7CL0zBApO1dssyNOqzG3VP/vk1JGg== |
| Return-Path | <ian.g.kelly@gmail.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.163 |
| X-Spam-Level | * |
| X-Spam-Evidence | '*H*': 0.68; '*S*': 0.00; 'dict': 0.09; 'subclass': 0.09; 'subject:Object': 0.09; 'def': 0.13; 'variables': 0.15; '2016': 0.16; 'composition.': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:between': 0.16; 'subject:usage': 0.16; 'way;': 0.16; 'wrote:': 0.16; 'variable': 0.18; 'keys': 0.22; 'code.': 0.23; 'seems': 0.23; "haven't": 0.24; 'header:In- Reply-To:1': 0.24; 'discussion': 0.24; "doesn't": 0.26; 'sense': 0.26; 'question': 0.27; 'message-id:@mail.gmail.com': 0.27; 'object,': 0.27; 'values': 0.28; '"do': 0.29; 'dictionary': 0.29; 'such.': 0.29; "i'm": 0.30; 'probably': 0.31; 'skip:_ 10': 0.32; 'subject:) ': 0.32; 'class': 0.33; 'tue,': 0.34; 'gets': 0.35; 'received:google.com': 0.35; 'ahead': 0.35; 'should': 0.36; 'instead': 0.36; 'received:209.85': 0.36; 'to:addr:python-list': 0.36; 'subject:?': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'say': 0.37; 'received:209.85.213': 0.37; 'seem': 0.37; 'things': 0.38; 'difference': 0.38; 'received:209': 0.38; 'test': 0.39; 'to:addr:python.org': 0.40; 'some': 0.40; 'questions': 0.40; 'your': 0.60; 'no.': 0.62; 'more': 0.63; 'great': 0.63; 'other.': 0.64; 'internet': 0.70; '26,': 0.72; 'yourself': 0.73; 'dict.': 0.84; 'etc..': 0.84; 'free:': 0.84; 'or:': 0.84; 'prevailing': 0.84; 'to:name:python': 0.84; 'approach.': 0.91; 'discerning': 0.91; 'ethan': 0.91; 'furman': 0.91; 'played': 0.91; 'subject:Class': 0.91; 'inheritance,': 0.93 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to; bh=PKYgIXAoV3JI2tCS4VbLxij8GoGkzRoS09AAkVebDA4=; b=LFmAL07BqBxhKDjapztnvnDJWBbIE09EpvR9zYbsPPHLScTCSqqnXRfwoQ8BmE2Yhm KcUKnb2yb8p4tlNxZwkeX74lHKrPyOPm1KhWNb5RIhYJL2DV0i8t96ZqlvNoRcSWEk8u y6tzlkpPzcmI1tlnUdEnTpimsYgPCm4zlVuU9sk71St3ukfLx1gJqKYn/SOscCd3x2U+ DF5qQEgI46whcKp7ssajBQQdCid30e59Czlk28tAft7NdEt1fSyOCG3uh6shR7lEiywX QM4XJw7tSpALBtgGd3JGUJbAikI6F/eHok0NwF39OxtxqRmjyVxschlqsX2tmQitdEBR 3KQg== |
| X-Google-DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to; bh=PKYgIXAoV3JI2tCS4VbLxij8GoGkzRoS09AAkVebDA4=; b=FqrkRhrZx4X+P9md1MhSYEdPtllE+ErXuVZS9fpCoJdiX9e5ffs7lG3ksbR+1hhcYn UBEbuS6oBXqGhy7b5lU5DXab8Rqqt1kEqn+BrDlsJg3CQxdXL8kavnvqJtXxTdroYc/g pjcxaJv21XwNG87kd5xL5OplwjLuau0t8bXBZO2ejTFLH+DWCyHP+37+Oaa7bOCkgqsV DAaOSj8f0OyA+Drl3x7LypenJ9gU9SFn/ilP+UdY+mh3M/HeEtQgD/h702nt8L/BS+99 dIKANP/kgcjpBQo2lvGNe8SUhaFopE4UlRN4fTjcmuy1qJCH5ofQ8XuxXRH2g3qCMq8i VBjg== |
| X-Gm-Message-State | AOPr4FUj1z2cJaFJtpB7UOZwu8BcG7cuBwQOMjBiclm3NSCvzK6hq7FYUga2dNvjCtDmGb1NB+4zMOKmk8Ta3w== |
| X-Received | by 10.50.24.131 with SMTP id u3mr11140620igf.93.1461767081514; Wed, 27 Apr 2016 07:24:41 -0700 (PDT) |
| In-Reply-To | <57204EA6.7060007@stoneleaf.us> |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.22 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| X-Mailman-Original-Message-ID | <CALwzidnr_O3m3=7RGUD1pu0Op93pZSLXKOMjHbecKRBeYn_30A@mail.gmail.com> |
| X-Mailman-Original-References | <5720357B.4060009@icloud.com> <57204EA6.7060007@stoneleaf.us> |
| Xref | csiph.com comp.lang.python:107724 |
Show key headers only | View raw
On Tue, Apr 26, 2016 at 11:31 PM, Ethan Furman <ethan@stoneleaf.us> wrote:
> 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..
You get those for free either way; in the first case you just have use
self.state.keys() instead of self.keys().
The question boils down to composition versus inheritance, and the
prevailing wisdom is to prefer composition. For me the discerning test
is "do the things that are in this dict make sense as items contained
by my object?" If the answer is yes, then go ahead and subclass from
dict. More often, the answer is no.
Some other great questions to ask yourself are "do I really want
len(my_object) to return the number of items in this dict" and "do I
really want list(my_object) to return all the keys in this dict"? If
the answer to all those is yes, then it's probably fair to say that
your object is-a dict and should be modeled as such.
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Differences between Class(Object) and Class(Dict) for dictionary usage? Ian Kelly <ian.g.kelly@gmail.com> - 2016-04-27 08:24 -0600
csiph-web