Some syntactic sugar proposals

John Nagle nagle at animats.com
Mon Nov 15 21:44:05 CET 2010


On 11/14/2010 11:30 PM, alex23 wrote:
> On Nov 15, 4:39 pm, Dmitry Groshev<lambdadmi... at gmail.com>  wrote:
>> First of all: how many times do you write something like

>
>> Second, I saw a lot of questions about using dot notation for a
>> "object-like" dictionaries and a lot of solutions like this:
>>      class dotdict(dict):
>>          def __getattr__(self, attr):
>>              return self.get(attr, None)
>>          __setattr__= dict.__setitem__
>>          __delattr__= dict.__delitem__
>> why there isn't something like this in a standart library?
>
> Personally, I like keeping object attribute references separate from
> dictionary item references.

     Right.  This isn't JavaScript.  If you need a "dict", use a
"dict".  Don't use attributes as named storage. It leads to
problems.  Functions and various built-in objects are in the
attribute namespace, and this can lead to name clashes.
Maybe security holes, if the attribute keys come from
external input.  There are also some restrictions on
the syntax of attribute names, restrictions "dict"
does not have.

     Remember, you can inherit from "dict", which
allows you to write

	obj['abc']

which is almost as short as
	
	obj.abc

but safer.

				John Nagle



More information about the Python-list mailing list