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


Groups > comp.lang.python > #7441

Re: Square bracket and dot notations?

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <bahamutzero8825@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'python.': 0.04; 'beginner': 0.04; 'dictionary': 0.07; 'interpreter': 0.07; 'python': 0.08; 'dict': 0.09; 'integers': 0.09; 'namespace': 0.09; 'values,': 0.09; 'this:': 0.10; 'am,': 0.14; 'received:209.85.214.174': 0.14; 'received:mail- iw0-f174.google.com': 0.14; 'wrote:': 0.14; 'dictionaries': 0.16; 'it).': 0.16; 'lookup': 0.16; 'notations': 0.16; 'object,': 0.19; 'prototype': 0.19; 'simpler': 0.19; '(which': 0.20; 'header:In- Reply-To:1': 0.21; 'variable': 0.21; 'seems': 0.21; 'keys': 0.23; 'stores': 0.23; 'objects': 0.23; '(or': 0.24; 'values': 0.25; 'object': 0.26; 'pass': 0.27; 'example': 0.27; "i'm": 0.27; '(not': 0.28; 'received:209.85.214': 0.28; 'subject:?': 0.29; 'variables': 0.29; 'lists': 0.29; 'class': 0.29; 'all,': 0.30; 'tuples': 0.30; 'print': 0.31; 'someone': 0.33; 'to:addr:python- list': 0.33; 'however,': 0.34; 'skip:# 10': 0.34; 'header:User- Agent:1': 0.35; 'message-id:@gmail.com': 0.36; '(with': 0.36; 'hold': 0.36; 'several': 0.36; 'received:google.com': 0.37; 'received:209.85': 0.37; 'case': 0.37; 'subject:: ': 0.38; 'received:192': 0.38; 'received:209': 0.39; 'sets': 0.39; 'to:addr:python.org': 0.39; 'entirely': 0.40; 'received:192.168.1': 0.40; 'more': 0.60; 'your': 0.60; 'chain': 0.66; 'square': 0.67
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:message-id:date:from:user-agent:mime-version:to :subject:references:in-reply-to:x-enigmail-version:content-type :content-transfer-encoding; bh=3JciX3yP8plrLRqeWSafM+w2LbZYpvroJEWrseLgsNw=; b=KGNwprHmw8EikWot1YYHb0LsUq9y2dfI/5X889NDfRO7HfmaMZYHnxXtC43W71fkcQ hpBQVyDkjJ2Pl48K5phSaK2wSBpvYzzNoQxNa25lVH1DwFn6oBExRGQZTAZ0+u4pwlvF rdSvRWM4qUS6Esp1EKkVFNovyB/rPpBDt3B6c=
DomainKey-Signature a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:user-agent:mime-version:to:subject:references :in-reply-to:x-enigmail-version:content-type :content-transfer-encoding; b=ohmm6akgH3mCvt9kMAFDTV+x+WtJHbx6DKPP8+2iOudybRqbtRSCa80s/epp/aN6jt Dx2/W0iO0neChre8E0kUl6sIlwVR+JTJCD0mDSsngUCHcgl5ITfdblLcUi6i+KoFjxcx dN42p+5o8boaIOZ+FxCOqGHE3eGrWdlJ45wIM=
Date Sat, 11 Jun 2011 05:06:27 -0500
From Andrew Berg <bahamutzero8825@gmail.com>
User-Agent Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.15) Gecko/20110303 Thunderbird/3.1.9 ThunderBrowse/3.3.5
MIME-Version 1.0
To "comp.lang.python" <python-list@python.org>
Subject Re: Square bracket and dot notations?
References <4ab9f6bd-cf2d-4c0a-8eda-7d8ffa6bd6c4@v10g2000yqn.googlegroups.com>
In-Reply-To <4ab9f6bd-cf2d-4c0a-8eda-7d8ffa6bd6c4@v10g2000yqn.googlegroups.com>
X-Enigmail-Version 1.1.1
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding 7bit
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
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.127.1307786802.11593.python-list@python.org> (permalink)
Lines 42
NNTP-Posting-Host 82.94.164.166
X-Trace 1307786803 news.xs4all.nl 49180 [::ffff:82.94.164.166]:54159
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:7441

Show key headers only | View raw


On 2011.06.11 04:41 AM, Asen Bozhilov wrote:
> Hi all,
> I am beginner in Python. What is interesting for me is that Python
> interpreter treats in different way dot and square bracket notations.
> I am coming from JavaScript where both notations lead prototype chain
> lookup.
>
> In Python it seems square bracket and dot notations lead lookup in
> different "store".
>
> Simple example with dict object:
>
> d = {"key" : "value"}
>
> print d["key"] #value
>
> print d.key #AttributeError
d is this case is a dictionary object, and therefore has keys you can
look up (with square brackets). The same is true with lists and tuples
(which have integers as "keys"). An arbitrary object can have arbitrary
values in arbitrary variables in its namespace (accessed with dots).
Objects can have a __dict__ variable that stores the variables in their
namespace as a dictionary (not entirely sure how this works; I'm sure
someone can expand on it).

With:
class simpleObject():
    pass
a = simpleObject()

This:
a.key = 'value'
a.otherkey = 'othervalue'

I simpler than:
a.props = {}
a.props['key'] = 'value'
a.props['otherkey'] = 'othervalue'


However, if you want your object to hold several different sets of keys
and respective values, dictionaries (or lists/tuples) make more sense.

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


Thread

Square bracket and dot notations? Asen Bozhilov <asen.bozhilov@gmail.com> - 2011-06-11 02:41 -0700
  Re: Square bracket and dot notations? Andrew Berg <bahamutzero8825@gmail.com> - 2011-06-11 05:06 -0500
  Re: Square bracket and dot notations? Ben Finney <ben+python@benfinney.id.au> - 2011-06-11 20:11 +1000
  Re: Square bracket and dot notations? Francesco Bochicchio <bieffe62@gmail.com> - 2011-06-11 03:46 -0700
    Re: Square bracket and dot notations? Asen Bozhilov <asen.bozhilov@gmail.com> - 2011-06-11 07:40 -0700
      Re: Square bracket and dot notations? Terry Reedy <tjreedy@udel.edu> - 2011-06-11 15:49 -0400
        Re: Square bracket and dot notations? Asen Bozhilov <asen.bozhilov@gmail.com> - 2011-06-11 13:41 -0700

csiph-web