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


Groups > comp.lang.python > #70531

Re: object().__dict__

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder1.xlned.com!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.051
X-Spam-Evidence '*H*': 0.90; '*S*': 0.00; '"""': 0.07; 'attribute': 0.07; 'attributes': 0.09; 'instances.': 0.09; 'pavel': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.11; 'attribute,': 0.16; 'attributes.': 0.16; 'finney': 0.16; 'object()': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'subject:object': 0.16; 'types,': 0.16; 'user-defined': 0.16; 'discussion': 0.18; '>>>': 0.22; 'header:User-Agent:1': 0.23; 'header:X-Complaints-To:1': 0.27; '"",': 0.31; 'default,': 0.31; 'writes:': 0.31; 'file': 0.32; 'class': 0.32; 'url:python': 0.33; '(most': 0.33; 'but': 0.35; 'instances': 0.36; 'url:org': 0.36; 'list': 0.37; 'ben': 0.38; 'mapping': 0.38; 'to:addr:python-list': 0.38; 'that,': 0.38; 'recent': 0.39; 'ability': 0.39; 'explain': 0.39; 'does': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'even': 0.60; 'skip:u 10': 0.60; 'new': 0.61; 'url:3': 0.61; 'simple': 0.61; 'provide': 0.64; 'different': 0.65; 'skip:\xe2 10': 0.65; 'social': 0.69; 'default': 0.69; '8bit%:43': 0.74; '8bit%:46': 0.78; 'lack': 0.78; 'gain': 0.79; '"spam"': 0.84; "'object'": 0.84; 'god,': 0.84; 'received:125': 0.84; 'url:datamodel': 0.84; 'url:reference': 0.84; '8bit%:18': 0.93
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Ben Finney <ben@benfinney.id.au>
Subject Re: object().__dict__
Date Wed, 23 Apr 2014 16:09:14 +1000
References <51d9b7f1-3511-4110-adb2-aa2226bd7a3c@lists.xtsubasa.org>
Mime-Version 1.0
Content-Type text/plain; charset=utf-8
Content-Transfer-Encoding 8bit
X-Gmane-NNTP-Posting-Host jigong.madmonks.org
X-Public-Key-ID 0xAC128405
X-Public-Key-Fingerprint 517C F14B B2F3 98B0 CB35 4855 B8B2 4C06 AC12 8405
X-Public-Key-URL http://www.benfinney.id.au/contact/bfinney-pubkey.asc
X-Post-From Ben Finney <bignose+hates-spam@benfinney.id.au>
User-Agent Gnus/5.13 (Gnus v5.13) Emacs/23.4 (gnu/linux)
Cancel-Lock sha1:sGarRpeD2X+JMIbC6qMTvl2KqfE=
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
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>
Newsgroups comp.lang.python
Message-ID <mailman.9453.1398233367.18130.python-list@python.org> (permalink)
Lines 55
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1398233367 news.xs4all.nl 2831 [2001:888:2000:d::a6]:37501
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:70531

Show key headers only | View raw


Pavel Volkov <sailor@lists.xtsubasa.org> writes:

> The attribute list is different now and there's no __dict__ and the
> object does not accept new attributes.
> Please explain what's going on.

It's a leaky abstraction, unfortunately.

By default, all user-defined types will provide their instances with a
‘__dict__’ attribute, whic is a mapping to store the instance's
attributes.

But some types don't have that, and ‘object’ is one of them. It
deliberately overrides the default behaviour, and has no ‘__dict__’ for
its instances.

    >>> foo = object()

    >>> foo.bar = "spam"
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    AttributeError: 'object' object has no attribute 'bar'

    >>> foo.__dict__
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    AttributeError: 'object' object has no attribute '__dict__'

Your user-defined types, even though they inherit from ‘object’, will
get a ‘__dict__’ as normal::

    >>> class Bag:
    >>>     """ A simple type to hold attributes. """

    >>> Bag.__mro__
    (<class '__main__.Bag'>, <class 'object'>)

    >>> foo = Bag()
    >>> foo.bar = "spam"
    >>> foo.__dict__
    {'bar': 'spam'}

See the discussion of ‘__slots__’, and note also that it's not
recommended to use this unless you know exactly why you need it
<URL:https://docs.python.org/3/reference/datamodel.html#slots>.

I consider it a wart of Python that its ‘object’ instances lack the
ability to gain arbitrary attributes in the way you expect.

-- 
 \      “Every man would like to be God, if it were possible; some few |
  `\          find it difficult to admit the impossibility.” —Bertrand |
_o__)                    Russell, _Power: A New Social Analysis_, 1938 |
Ben Finney

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


Thread

Re: object().__dict__ Ben Finney <ben@benfinney.id.au> - 2014-04-23 16:09 +1000

csiph-web