Small Troll on notation of variables over time

Hendrik van Rooyen mail at microcorp.co.za
Sat Aug 19 06:49:37 EDT 2006


Hi there,

I can write:

s = 'some string'
then print s[1] will be the string 'o'

and a while later I can write:

s = 'other some string'
then print s[1] will be the string 't'

and then:

s = [1,2,3,4]
then print s[1] will be the number 2

and still later:

s = {1:'boo',2:'foo',3:'shoo'}
when print s[1] will yield the string 'boo'

Now how about introducing an index that works over time,
such that s{0} (the default so as to not break any existing code) 
implies the current object bound to the name s,
with s{1} being the previous one, and so on...

This will mean that s{2}[1] is the string 't'
and s{3}[1] is the string 'o'
while s{4} should be None...

It should be easy to implement this, as all that needs to be done is to 
change the "pointer" (or whatever) to the object with a stack of them
at the time the binding or rebinding is done...

I first thought of using s{-1} for the previous "value" but that 
suffers from the regrettable disadvantage that it implies the 
existence of an s{1} - i.e. a future value - and I could not think
of a way to achieve this - so the minus sign adds no information 
and should therefore be left out...

What do you guys think?

- Hendrik




More information about the Python-list mailing list