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


Groups > comp.lang.python > #29532

Re: Using dict as object

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!ecngs!feeder2.ecngs.de!newsfeed.freenet.ag!news2.euro.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!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.004
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'python.': 0.02; 'nested': 0.07; 'indirectly': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'dictionaries': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'subject:object': 0.16; 'wrote:': 0.17; 'basically': 0.17; 'equivalent': 0.20; 'proposed': 0.20; 'regardless': 0.21; 'assignment': 0.22; 'statement': 0.23; 'header:User-Agent:1': 0.26; 'implemented': 0.27; 'skip:b 30': 0.27; 'question': 0.27; 'header:X-Complaints-To:1': 0.28; 'statements': 0.29; 'convert': 0.29; 'handled': 0.29; 'skip:_ 10': 0.29; 'class': 0.29; 'skip:- 30': 0.31; 'problem.': 0.32; 'to:addr:python-list': 0.33; 'skip:- 20': 0.34; 'list': 0.35; 'fail': 0.35; 'there': 0.35; 'received:org': 0.36; 'but': 0.36; 'others.': 0.36; 'does': 0.37; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'your': 0.60; 'first': 0.61; 'referred': 0.62; 'times': 0.63; '"recursive': 0.84; 'oscar': 0.84; 'subject:Using': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Oscar Benjamin <oscar.j.benjamin@gmail.com>
Subject Re: Using dict as object
Date Wed, 19 Sep 2012 23:04:26 +0000 (UTC)
References <345da32c-a22b-4025-bb50-794d684885b1@googlegroups.com> <5059B062.8020108@davea.name> <CAJ+soVegTjd_O-UYx8t400CFxXgd2B=ngt9JNzWJUVq+YNvXJA@mail.gmail.com>
X-Gmane-NNTP-Posting-Host cpc1-aztw8-0-0-cust1455.18-1.cable.virginmedia.com
User-Agent slrn/pre1.0.0-18 (Linux)
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 <http://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 <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.945.1348095884.27098.python-list@python.org> (permalink)
Lines 42
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1348095884 news.xs4all.nl 6885 [2001:888:2000:d::a6]:41946
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:29532

Show key headers only | View raw


On 2012-09-19, Pierre Tardy <tardyp@gmail.com> wrote:
> --===============1362296571==
> Content-Type: multipart/alternative; boundary=bcaec554d3229e814204ca105e50
>
> --bcaec554d3229e814204ca105e50
> Content-Type: text/plain; charset=ISO-8859-1
>
>>
>>  This has been proposed and discussed and even implemented many
>> times on this list and others.
>>
> I can find this question on SO
> http://stackoverflow.com/questions/4984647/accessing-dict-keys-like-an-attribute-in-python
> which is basically answered with this solution
>
> class AttributeDict(dict):
>     __getattr__ = dict.__getitem__
>     __setattr__ = dict.__setitem__
>
>
> but this does not allow recursive access, you would need to first convert
> all nested dictionaries to AttributeDict.
> a.b.c.d = 2 # fail
> a.b = dict(c=3)
> a.b.c=4 # fail

There is no way to control "recursive access" in Python. The statement

a.b.c = 2

is equivalent to the statements

o = a.b   # o = a.__getattr__('b')
o.c = 2   # o.__setattr__('c', 2)

The way that the o.c assignment is handled is determined by the type of o
regardless of the type of a. If you're looking for a way to change only the
type of a and make a custom __(set|get)attr__ work for all dicts that are
indirectly referred to then there is no solution to your problem.

Oscar

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


Thread

Using dict as object Pierre Tardy <tardyp@gmail.com> - 2012-09-19 03:24 -0700
  Re: Using dict as object Dave Angel <d@davea.name> - 2012-09-19 07:45 -0400
  Re: Using dict as object Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2012-09-19 15:02 +0200
  Re: Using dict as object Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2012-09-19 14:04 +0000
  Re: Using dict as object Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2012-09-19 23:04 +0000

csiph-web