using __getitem()__ correctly

Ben Finney ben+python at benfinney.id.au
Wed Dec 30 19:21:59 EST 2015


Steven D'Aprano <steve at pearwood.info> writes:

> On Thu, 31 Dec 2015 10:13 am, Ben Finney wrote:
>
> > 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.
>
> I'm not sure what distinction you're referring to, can you explain?

Tersely: the relationship between an object and its attributes, is not
the same as the relationship between a dictionary and its items.

> Obviously there is a syntax difference between x.attr and x['key']

Not merely syntax; the attributes of an object are not generally
available as items of the container.

> but attributes *are* items in a dictionary

That's like saying everything in Python is a number: it conflates the
implementation with the semantics.

The distinction between a Python integer and a Python boolean value is
real and important, despite the incidental fact of their both being
implemented as numbers.

> Either the instance __dict__, the class __dict__, or a superclass
> __dict__.

No, I'm not referring to the ‘__dict__’ attribute of an object; I'm
referring to the object itself.

To talk about the attributes of an object ‘foo’ is distinct from talking
about the items in a dictionary ‘foo’. That distinction is real, and
important.

-- 
 \               “… correct code is great, code that crashes could use |
  `\           improvement, but incorrect code that doesn’t crash is a |
_o__)                    horrible nightmare.” —Chris Smith, 2008-08-22 |
Ben Finney




More information about the Python-list mailing list