
Steven D'Aprano wrote:
On Wed, Dec 09, 2020 at 12:05:17PM -0000, Mathew Elman wrote:
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. I would say, don't do that. If you have a list, and you pass it to another function after converting to a tuple, why would you take that tuple and convert back to a list when you already have a list? For types like casting back and forth between float and int, there will be data loss. Even if there is not, the overhead of creating a "lazy proxy" to the float is probably greater than just converting. I see what you mean here, I think my initial thought was more towards container types rather than ints and floats, I said these should be included because I saw no reason why not. Thinking about it, for int and float types it makes less sense, because you have no great saving, whereas for a container type, you can increase the reference count by 1 instead of N.
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. You seem to be referring to the lazy_tuple as the hook, I am saying that _I_ could implement a lazy class if hooks existed. i.e. I could make a class `LazyTuple` that when instanced with a list, creates and stores the hooks on itself.
How is this supposed to work in practice? A method e.g. `create_hook(method, callback)` would return a concrete reference to the hook, and uses a weak reference to know if it needs to execute the callback (meaning that it would only add the overhead of a check for callbacks if the concrete reference still existed).
Where does the hook live? When is it called? What does it do? How does the lazy tuple know that the list's setitem has been called?
It knows because of a hook? This is my whole point about including hooks in this thread, i.e. if it was possible to add a callback on a method, it would execute when it is called. I am not sure I understand your question.