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

Antoine Pitrou solipsis at pitrou.net
Mon Jan 30 16:06:56 CET 2012


On Mon, 30 Jan 2012 08:58:46 -0600
Massimo Di Pierro
<massimo.dipierro at gmail.com> 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')
> 
> mydict.__getattr__ is the single most called method in web2py and it does affect performance. It it were supported natively by the language we would benefit from it.

The easy and Pythonic way to benefit from native performance is to
use a dict instead...

Regards

Antoine.





More information about the Python-ideas mailing list