using __getitem()__ correctly

Ben Finney ben+python at benfinney.id.au
Thu Dec 31 06:51:47 EST 2015


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

> On Thu, 31 Dec 2015 11:21:59 +1100, Ben Finney wrote:
>
> > Tersely: the relationship between an object and its attributes, is
> > not the same as the relationship between a dictionary and its items.
>
> I understand this to mean that the relationship between a dictionary
> and its items is less complex than the relationship between an object
> and its attributes.

That's not implied by the above, nor is it what I meant.

I meant that the relationship is not the same.

* The attributes of an object ‘foo’ are what you access via ‘foo.bar’
  syntax. You don't access those attributes via ‘foo[bar]’.

* The items of a dictionary ‘foo’ are what you access via ‘foo[bar]’
  syntax. You don't access those items via ‘foo.bar’.

That's an important, and real, difference. And it's much better learned
as part of a comprehensive course on Python, not in a deep dive into the
magic methods.

I really don't know why it's been so difficult to talk about this, but I
hope my meaning is clear now.

Have a wonderful end of year, folks.

-- 
 \              “There are no chaplains in foxholes.” —Sergeant Justin |
  `\                                              Griffith, 2011-07-27 |
_o__)                                                                  |
Ben Finney




More information about the Python-list mailing list