using __getitem()__ correctly

Ben Finney ben+python at benfinney.id.au
Wed Dec 30 18:13:53 EST 2015


"Charles T. Smith" <cts.private.yahoo at gmail.com> writes:

> I don't understand this distinction between an "attribute" and a "dict
> item".

When did you most recently work through the Python tutorial
<URL:https://docs.python.org/3/tutorial/>> You may want to work through
it again, from start to finish and exercising each example, to be sure
you have a solid understanding of basic concepts like these.

In brief: Objects have attributes; looking up an attribute on an object
has specific syntax. Dictionaries are collections of items; the items
are looked up by key, using a quite different syntax. Those two
different syntaxes translate to distinct special methods.

You may be familiar with other languages where the distinction between
“attribute of an object” is not distinct from “item in a dictionary”.
Python is not one of those languages; the distinction is real and
important. You'll need to do some remedial learning of Python, and I
recommend working through the Python tutorial.

-- 
 \            “But it is permissible to make a judgment after you have |
  `\    examined the evidence. In some circles it is even encouraged.” |
_o__)                    —Carl Sagan, _The Burden of Skepticism_, 1987 |
Ben Finney




More information about the Python-list mailing list