Alternate Syntax for dictionary elements

Steve Holden sholden at
Tue Jul 3 15:11:06 EDT 2001

"Gerson Kurz" <gerson.kurz at> wrote in message
news:3b421046.1836046 at
> 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.
And namespaces are, in fact, for the most part dictionaries. There are some
optimzations, but that needn't concern us unduly here.

> 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).
Readability is the key to maintainability, which is highly desirable.

> 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 ?
Definitely not total crap, and in fact a quite often-used paradigm. But not
with dictionaries. Of course, with the prospect that the type/class
dichotomy may be resolved in a forthcoming release, you could make your
dictionaries behave that way if you wanted to. But normally such trickery is
should be unnecessary, because Python already has things which behave in the
way you require: instances (each of which has its own namespace. Geddit?)

Question: if you're prepared to accept the restrictions you list above, why
not just use a class instance instead? Consider:

class Mdict:

dict = Mdict() = 3.45
dict.two = 7.9

... etc.


More information about the Python-list mailing list