On 11/14/2010 11:30 PM, alex23 wrote:
On Nov 15, 4:39 pm, Dmitry Groshev<lambdadmi...@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
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to