Is it possible to have a list be evaluated lazily in Python?
For example
a = 1
list = [a]
print list
#[1]
a = 2
print list
#[1]
If the list was set to evaluate lazily then the final line would be [2]
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The concept of “lazy” evaluation normally comes with functional languages — but in those you could not reassign two different values to the same identifier, so, not even there could your example be reproduced.
The point is not about laziness at all — it is that using an identifier is guaranteed to be identical to getting a reference to the same value that identifier is referencing, and re-assigning an identifier, a bare name, to a different value, is guaranteed to make the identifier refer to a different value from them on. The reference to the first value (object) is not lost.
Consider a similar example where re-assignment to a bare name is not in play, but rather any other kind of mutation (for a mutable object, of course — numbers and strings are immutable), including an assignment to something else than a bare name:
Since there is no
a - ...that reassigns the bare namea, but rather ana[:] = ...that reassignsa‘s contents, it’s trivially easy to make Python as “lazy” as you wish (and indeed it would take some effort to make it “eager”!-)… if laziness vs eagerness had anything to do with either of these cases (which it doesn’t;-).Just be aware of the perfectly simple semantics of “assigning to a bare name” (vs assigning to anything else, which can be variously tweaked and controlled by using your own types appropriately), and the optical illusion of “lazy vs eager” might hopefully vanish;-)