FAQ
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

Search Discussions

  • Chris Johnson at Aug 19, 2006 at 12:18 pm

    Hendrik van Rooyen wrote:
    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?
    I think it's pointless. If you want access to more than one of these
    variables, don't use the same name.

    That and if a symbol is still in scope, then all of its previous values
    still have active references, so they won't get deallocated by the
    garbage collector, meaning a lot of overhead that most people will not
    take advantage of.
  • John Machin at Aug 19, 2006 at 12:34 pm
    Hendrik van Rooyen wrote:
    [snip]
    What do you guys think?
    The subject said it all. You should find some other way of entertaining
    yourself on the weekends :-)
  • Hendrik van Rooyen at Aug 19, 2006 at 2:11 pm
    "John Machin" wrote:


    Hendrik van Rooyen wrote:
    [snip]
    What do you guys think?
    The subject said it all. You should find some other way of entertaining
    yourself on the weekends :-)
    This is the right answer...

    *grin* - well - at least you *were* warned... - Hendrik
  • Gabriel Genellina at Aug 19, 2006 at 12:35 pm

    At Saturday 19/8/2006 07:49, Hendrik van Rooyen wrote:
    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...
    Doing that *always* for *all* names would be ridiculous - objects
    would never get garbage collected, by example. And
    99.99999999999999999999% of programs don't need that behavior at all.
    So just implement that for your use case - if you have any!



    Gabriel Genellina
    Softlab SRL





    __________________________________________________
    Pregunt?. Respond?. Descubr?.
    Todo lo que quer?as saber, y lo que ni imaginabas,
    est? en Yahoo! Respuestas (Beta).
    ?Probalo ya!
    http://www.yahoo.com.ar/respuestas
  • Jeremy Sanders at Aug 21, 2006 at 10:36 am

    Hendrik van Rooyen wrote:

    What do you guys think?
    You could get something similar using an object, such as

    class Hist(object):

    def __init__(self):
    self.vals = [None]

    def __call__(self, index=-1):
    return self.vals[index]

    def set(self, val):
    self.vals.append(val)

    a = Hist()

    a.set(5)
    print a()

    a.set('hi there')
    print a()
    print a(-2)

    Which prints
    5
    hi there
    5
  • Hendrik van Rooyen at Aug 21, 2006 at 12:41 pm

    "Jeremy Sanders" wrote:
    Hendrik van Rooyen wrote:
    What do you guys think?
    You could get something similar using an object, such as

    class Hist(object):

    def __init__(self):
    self.vals = [None]

    def __call__(self, index=-1):
    return self.vals[index]

    def set(self, val):
    self.vals.append(val)

    a = Hist()

    a.set(5)
    print a()

    a.set('hi there')
    print a()
    print a(-2)

    Which prints
    5
    hi there
    5
    - Sneaky! - I like this....

    - Hendrik
  • Piet van Oostrum at Aug 21, 2006 at 10:52 am
    You are about 7 months early.
    --
    Piet van Oostrum <piet at cs.uu.nl>
    URL: http://www.cs.uu.nl/~piet [PGP 8DAE142BE17999C4]
    Private email: piet at vanoostrum.org
  • Hendrik van Rooyen at Aug 21, 2006 at 12:48 pm

    "Piet van Oostrum" Wrote:
    You are about 7 months early.
    --
    Am I? - Early for what - a seven months premature baby is small indeed...

    - Hendrik

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedAug 19, '06 at 10:49a
activeAug 21, '06 at 12:48p
posts9
users6
websitepython.org

People

Translate

site design / logo © 2018 Grokbase