setattr() oddness
Steven D'Aprano
steven at REMOVE.THIS.cybersource.com.au
Mon Jan 18 01:47:59 EST 2010
On Mon, 18 Jan 2010 07:25:58 +0100, Dieter Maurer wrote:
> 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.
(1) Wouldn't it be more natural to store these query keys in a list or
dictionary rather than as attributes on an object?
e.g. instead of:
cache.__setattr__('complex query object', value)
use:
cache['complex query object'] = value
(2) How does __setattr__ let you control access to the object? If a user
wants to modify the cache, and they know the complex query object, what's
stopping them from using __setattr__ too?
--
Steven
More information about the Python-list
mailing list