Steven D'Aprano wrote:
> On Tue, Dec 08, 2020 at 11:46:59AM -0000, Mathew Elman wrote:
> > I would like to propose adding lazy types for casting
> > builtins in a 
> > lazy fashion. e.g. lazy_tuple which creates a reference to the 
> > source iterable and a morally immutable sequence but only populates 
> > the tupular container when it or the source is used.
> > What are your use-cases for this?
> Does this include things like lazy_list, lazy_float,
> lazy_bool, 
> lazy_str, lazy_bytearray etc?

I would say yes, it should include these types as well. 
The use case is for when you are casting between types a high number of times 
to pass them around, especially into another type and back.

> > An alternative to adding lazy types / lazy type
> > casting (making it 
> > possible to implement these oneself) would be to add method call hooks 
> > to python, since this would allow having a "freeze value" callback 
> > hooked into the __setitem__ and __getitem__ methods.  Hooks may be a 
> > more useful solution for wider use cases as well.
> > Nope, sorry, I don't see how that would work. Here I have a list:
> L = [50, 40, 30, 20, 10]
> 
> Suppose these hooks exist. I want to make a "lazy tuple":
> t = lazy_tuple(L)
> 
> How do these hooks freeze the list?

they don't freeze the list, they freeze the values in the lazy_tuple when the 
list is mutated, so when mutating the list, the values in the tuple are set (if 
they have not been already), so that they aren't also mutated in the 
lazy_tuple. Of course for delete and insert this would mean you might have to 
"freeze" from where the insert/delete occurs to the end of the tuple but that 
is no different than using a normal tuple i.e. iterate over all elements. 
However for setitem where only position i is replaced, the tuple can set its 
value to the original without having to evaluate anything extra.

> What if I have more than one lazy object pointing at the same source?
> s = lazy_str(L)
> 
> And then follow with a different method call?
> L.insert(2, "surprise!")
> 
> I just can't see how this will work.

I don't follow what your point is, sorry. Are you saying that insert is another 
method that can update a list so delitem and setitem hooks would miss it?
The point I was making with hooks is that you _could_ write a lazy class that 
creates freezing hooks for any mutation method. But the hooks would be up to 
the coder to implement and hook into the correct mutation methods.

I also don't see an issue with multiple lazy types pointing to the same 
"source". It would just freeze the values in both of them when updating 
anything in the original.
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/YJ4GVP3KOB2WRU2EY3QO2XGNIFJAGZS2/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to