setattr() oddness

Dieter Maurer dieter at handshake.de
Mon Jan 18 01:25:58 EST 2010


Lie Ryan <lie.1296 at gmail.com> writes on Sat, 16 Jan 2010 19:37:29 +1100:
> On 01/16/10 10:10, Sean DiZazzo wrote:
> > Interesting.  I can understand the "would take time" argument, but I
> > don't see any legitimate use case for an attribute only accessible via
> > getattr().  Well, at least not a pythonic use case.
> 
> mostly for people (ab)using attributes instead of dictionary.

Here is one use case:

 A query application. Queries are described by complex query objects.
 For efficiency reasons, query results should be cached.
 For this, it is not unnatural to use query objects as cache keys.
 Then, query objects must not get changed in an uncontrolled way.
 I use "__setattr__" to control access to the objects.

Dieter



More information about the Python-list mailing list