Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #107725

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

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 2016-04-27 08:33 -0600
Message-ID <mailman.149.1461767662.32212.python-list@python.org> (permalink)
References <5720357B.4060009@icloud.com> <CALwzidn0SN5yax3bfmTPDTxBvfr+bnVNzeUe3dZYrS9ji2WCtw@mail.gmail.com>

Show all headers | View raw


On Tue, Apr 26, 2016 at 9:43 PM, Christopher Reimer
<christopher_reimer@icloud.com> wrote:
>     class Test2(dict):
>         def __init__(self):
>                 self.__dict__ = {'key', 'value'}

This class definition looks muddled. Because Test2 inherits from dict,
the object referred to by "self" will be a dict, and self.__dict__ is
actually a *different* dict, containing the attributes of self. The
line:

    self.__dict__ = {'key', 'value'}

is essentially equivalent to:

    self.key = value

and will be regardless of whether you inherit from object or dict. If
you find this distinction confusing, then I recommend not inheriting
from dict.

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Differences between Class(Object) and Class(Dict) for dictionary usage? Ian Kelly <ian.g.kelly@gmail.com> - 2016-04-27 08:33 -0600

csiph-web