Square bracket and dot notations?

Terry Reedy tjreedy at udel.edu
Sat Jun 11 15:49:36 EDT 2011


On 6/11/2011 10:40 AM, Asen Bozhilov wrote:

> It is exactly what I wanted to know. Thank you. I have not examined
> classes in Python yet, but when I do it I will understand some new
> things. One of the most interesting is, can an object inherit items
> trough the parent class? By items I mean items which are accessible
> trough square bracket notation.

.attributes are inherited. [index-or-key] items are not.

> I really like Pythonic way here. Square bracket and dot notations
> allow me to create an object which can be "true" hash map and
> meanwhile to support independent methods from its keys. I could have
> an item and a property with same names and they won't interfere each
> other.

Right. d.items is a dict method. d['items'] is whatever you assign.
Named tuples in the collections modules, which allow access to fields 
through .name as well as [index], have the name class problem. All the 
methods are therefore given leading underscore names to avoid this. [But 
there still could be a clash if someone used field names with leading 
underscores!]  Python reserves and uses __xxx__ for system names just to 
avoid clashes.

-- 
Terry Jan Reedy




More information about the Python-list mailing list