Alternate Syntax for dictionary elements

Gerson Kurz gerson.kurz at t-online.de
Tue Jul 3 14:45:05 EDT 2001


Please consider the following:

dict = { 'type' : 'button', 'id': 32, 'name' : 'some name' }
...
if o['type'] == 'button':
	# do something for objects of type button

Now, take a look at

if o.type == 'button':
	# do something for objects of type button

A dictionary can in many cases be interpreted as a dynamically created
object. And an object should have members; or rather, you should be
able to access its data in a member-style fashion.

This change doesn't save you much type overhead - but it makes the
code look more readable for those uses for which it makes sense to
treat dictionarys as dynamic objects (and I have lots of uses for
these). 

I realize that there are possible dictionary keys (such as tuples) for
which such a syntax is improper; it should be sufficient to make the
following restrictions:

- the syntax is only possible for string-typed keys that *qualify as
valid identifiers*
- the syntax is only possible for keys that are not part of dir({}). 

The implementation would be a hack that extendes the {}.__methods__
lookup. 

Worthwile pursuit or total crap ?




More information about the Python-list mailing list