Small Troll on notation of variables over time

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Hendrik van Rooyen

    #1

    Small Troll on notation of variables over time

    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

  • Chris Johnson

    #2
    Re: Small Troll on notation of variables over time


    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.

    Comment

    • John Machin

      #3
      Re: Small Troll on notation of variables over time


      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 :-)

      Comment

      • Hendrik van Rooyen

        #4
        Re: Small Troll on notation of variables over time


        "John Machin" <sjmachin@lexic on.netwrote:



        |
        | 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

        Comment

        • Jeremy Sanders

          #5
          Re: Small Troll on notation of variables over time

          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.appen d(val)

          a = Hist()

          a.set(5)
          print a()

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

          Which prints
          5
          hi there
          5

          --
          Jeremy Sanders

          Comment

          • Piet van Oostrum

            #6
            Re: Small Troll on notation of variables over time

            You are about 7 months early.
            --
            Piet van Oostrum <piet@cs.uu.n l>
            URL: http://www.cs.uu.nl/~piet [PGP 8DAE142BE17999C 4]
            Private email: piet@vanoostrum .org

            Comment

            • Hendrik van Rooyen

              #7
              Re: Small Troll on notation of variables over time

              "Piet van Oostrum" <piet@cs.uu.nlW rote:

              | You are about 7 months early.
              | --

              Am I? - Early for what - a seven months premature baby is small indeed...

              - Hendrik

              Comment

              • Hendrik van Rooyen

                #8
                Re: Small Troll on notation of variables over time

                "Jeremy Sanders" <jeremy+complan gpython@jeremys anders.netwrote :

                | 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.appen d(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

                Comment

                Working...