views:

51

answers:

2

View are useful constructions of Python 3. For those who never noticed (like me): for a dictionary d you can write k = d.keys() and even if you update d the variable k will still be giving you the updated keys. You can write then k1 & k2 and it will always give you d1.keys() & d2.keys()

I want to implement this for my personal todo manager, but I would like to make complex views dynamic, or lazily evaluated. That is, I have some views called so, post and priority and I want to be able to write:

    now = so | phone & priority

so that later, when the __repr__(now) is called, evaluation is performed only at that point.

My first thought was to overload the logical operators so I changed View.__and__ to create a new view that remembers itself being a composite of two subviews and applies & to them at computation. But there seem to be quite a lot of logical operators, so I'm not sure if I'm doing the right thing.

Is there a standard library class that would help me with that? How can I simplify the process?

+1  A: 

Well, there is a collection.UserList class which defines up most of them, perhaps that would mean you don't have to override all of them.

Lennart Regebro
+1  A: 

There is no "easy" way to do so, especially if you want the lazy behaviour as stated. But still, there aren't that many logical operators, only three of them: __and__, __or__ and __xor__.

(for additional efficiency you can optionally implement the in-place versions __iand__, __ior__ and __ixor__, but if you don't the normal versions will be invoked as a fallback)

Antoine P.