[Python-ideas] Dict-like object with property access

Masklinn masklinn at masklinn.net
Mon Jan 30 17:01:31 CET 2012


On 2012-01-30, at 15:58 , Massimo Di Pierro wrote:
> In web2py we have a class called Storage. (web.py has a similar class too).
> 
> works exactly like this except that if you do mydict.someprop and someprop does not exist returns None (which plays the role of JS undefined) instead of raining an exception. Users like this a lot because they can do:
> 
>    a = mydict.somevalue or 'somedefault'
> 
> which new users find more readable than
> 
>   a = mydict.get('somevalue','somedefault')

And with completely different semantics, as a falsy value (empty string, 0, empty collection, None, etc…) will yield the default for the first case, but not for the second case.


More information about the Python-ideas mailing list