Python Feature Request: Allow changing base of member indices to 1

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • samjnaa@gmail.com

    #1

    Python Feature Request: Allow changing base of member indices to 1

    This is like the previous one. Please check for sanity and approve for
    posting at python-dev.

    I would like to have something like "option base" in Visual Basic.
    IIRC it used to allow me to choose whether 0 or 1 should be used as
    the base of member indices of arrays. In Python, the same can be used
    with strings, lists, tuples etc.

    This would mean:
    foo = "foo"
    =foo[1] == 'f'

    foo = ['foo', 'bar', 'spam' ]
    =foo[1] == 'foo'

    foo = ('spam', 'eggs')
    =foo[1] == 'spam'

    For convenience it should also affect the range function so that:

    range(3) = [1, 2, 3]

    because this is often used where arrays would be used in VB.

    Finally, when the programmer does not specify his choice of base at
    the beginning of the program, the current behaviour of using 0 as base
    should continue so that there is no problem with backward
    compatibility.

  • =?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=

    #2
    Re: Python Feature Request: Allow changing base of member indicesto 1

    samjnaa@gmail.c om schrieb:
    This is like the previous one. Please check for sanity and approve for
    posting at python-dev.
    This one is not sane. It's not possible to change the indexing of
    objects on a per-module basis, as objects may cross module boundaries.

    Suppose you have this code:

    option base
    import sys
    print sys.path[1]

    So should the be 0-based (because path is in module sys), or should
    it be one-based (because the access occurs in a module that uses
    1-indexing)?

    Regards,
    Martin

    Comment

    • jamadagni

      #3
      Re: Python Feature Request: Allow changing base of member indices to 1

      On Apr 14, 4:01 pm, "Martin v. Löwis" <mar...@v.loewi s.dewrote:
      This one is not sane. It's not possible to change the indexing of
      objects on a per-module basis, as objects may cross module boundaries.
      I do not request for this to be changed per-module. Once I say
      something like:

      from __future__ import indices_start_a t_one

      afterwards I would expect *any* index from *any* module to start at 1.
      To my understanding, Python is going to parse the content of modules
      only as and when they are called. So Python is basically reading the
      content of those modules when the flag of indices_start_a t_one is
      active, and hence I would expect it to start counting at 1.

      I can envisage, stemming from your comments, that this can lead to
      trouble when a module being accessed has hard-coded indices. It would
      not be fair of me to expect all module writers to declare the base
      option at the head of each file, though it would be good practice. So
      the solution is to allow the programmer to choose to limit this tag
      locally or activate it globally.

      option base 1 local
      option base 1 global

      Comment

      • Marc 'BlackJack' Rintsch

        #4
        Re: Python Feature Request: Allow changing base of member indices to 1

        In <1176557999.665 663.299740@y80g 2000hsf.googleg roups.com>, jamadagni
        wrote:
        On Apr 14, 4:01 pm, "Martin v. Löwis" <mar...@v.loewi s.dewrote:
        >
        >This one is not sane. It's not possible to change the indexing of
        >objects on a per-module basis, as objects may cross module boundaries.
        >
        I do not request for this to be changed per-module. Once I say
        something like:
        >
        from __future__ import indices_start_a t_one
        >
        afterwards I would expect *any* index from *any* module to start at 1.
        To my understanding, Python is going to parse the content of modules
        only as and when they are called.
        Modules are parsed when they are imported. And some modules are already
        imported before your module is imported because they are built-in or
        loaded to be able to import your module in the first place. And what
        about modules that are written in C?

        Ciao,
        Marc 'BlackJack' Rintsch

        Comment

        • jamadagni

          #5
          Re: Python Feature Request: Allow changing base of member indices to 1

          Modules are parsed when they are imported. And some modules are already
          imported before your module is imported because they are built-in or
          loaded to be able to import your module in the first place. And what
          about modules that are written in C?
          OK fine. It is clear that this feature must be implemented if at all
          only on a per-module basis. So can we have votes for per-module
          implementation of this feature?

          Comment

          • ici

            #6
            Re: Python Feature Request: Allow changing base of member indices to 1

            On Apr 14, 1:27 pm, samj...@gmail.c om wrote:
            ....
            This would mean:
            foo = "foo"
            =foo[1] == 'f'
            >
            class Str1(str):
            def __getitem__(sel f,i):
            return str.__getitem__ (self,i-1)

            s1 = Str1("foo")
            print s1[1]

            Comment

            • Steve Holden

              #7
              Re: Python Feature Request: Allow changing base of member indices to 1

              jamadagni wrote:
              >Modules are parsed when they are imported. And some modules are already
              >imported before your module is imported because they are built-in or
              >loaded to be able to import your module in the first place. And what
              >about modules that are written in C?
              >
              OK fine. It is clear that this feature must be implemented if at all
              only on a per-module basis. So can we have votes for per-module
              implementation of this feature?
              >
              Like votes would make a difference. Please just accept that this is not
              a sensible suggestion and leave it at that.

              regards
              Steve
              --
              Steve Holden +44 150 684 7255 +1 800 494 3119
              Holden Web LLC/Ltd http://www.holdenweb.com
              Skype: holdenweb http://del.icio.us/steve.holden
              Recent Ramblings http://holdenweb.blogspot.com

              Comment

              • Mel Wilson

                #8
                Re: Python Feature Request: Allow changing base of member indices to 1

                jamadagni wrote:
                OK fine. It is clear that this feature must be implemented if at all
                only on a per-module basis. So can we have votes for per-module
                implementation of this feature?
                The only way that can work is if the API to the module doesn't expose
                ANY sequence indices. It would be a great pain to have to remember
                which of the modules you've imported expect base 0 and which expect
                base 1. (There's a Monty Python sketch: a station full of police
                officers who only understand you if you speak
                slowly/quickly/high-pitched/low-pitched/etc. depending on the
                individual officer.)

                The scheme could work if the module was carefully programmed to show
                and accept only application-relevant values that nobody would expect
                to have an alternative base -- quantities of things, arbitrary code
                numbers, and the like.

                Mel.

                Comment

                • faulkner

                  #9
                  Re: Python Feature Request: Allow changing base of member indices to 1

                  On Apr 14, 6:27 am, samj...@gmail.c om wrote:
                  This is like the previous one. Please check for sanity and approve for
                  posting at python-dev.
                  >
                  I would like to have something like "option base" in Visual Basic.
                  IIRC it used to allow me to choose whether 0 or 1 should be used as
                  the base of member indices of arrays. In Python, the same can be used
                  with strings, lists, tuples etc.
                  >
                  This would mean:
                  foo = "foo"
                  =foo[1] == 'f'
                  >
                  foo = ['foo', 'bar', 'spam' ]
                  =foo[1] == 'foo'
                  >
                  foo = ('spam', 'eggs')
                  =foo[1] == 'spam'
                  >
                  For convenience it should also affect the range function so that:
                  >
                  range(3) = [1, 2, 3]
                  >
                  because this is often used where arrays would be used in VB.
                  >
                  Finally, when the programmer does not specify his choice of base at
                  the beginning of the program, the current behaviour of using 0 as base
                  should continue so that there is no problem with backward
                  compatibility.
                  __future__ is used to access upcoming features, and changing the base
                  offset is not [and never will be] slated for future development. zero
                  has been used as the base offset in all real languages since the dawn
                  of time, and isn't something that can be changed without seriously
                  breaking heads.

                  i can't believe nobody's posted this yet:


                  Comment

                  • Paddy

                    #10
                    Re: Python Feature Request: Allow changing base of member indices to 1

                    On Apr 14, 11:27 am, samj...@gmail.c om wrote:
                    This is like the previous one. Please check for sanity and approve for
                    posting at python-dev.
                    >
                    I would like to have something like "option base" in Visual Basic.
                    IIRC it used to allow me to choose whether 0 or 1 should be used as
                    the base of member indices of arrays. In Python, the same can be used
                    with strings, lists, tuples etc.
                    >
                    This would mean:
                    foo = "foo"
                    =foo[1] == 'f'
                    >
                    foo = ['foo', 'bar', 'spam' ]
                    =foo[1] == 'foo'
                    >
                    foo = ('spam', 'eggs')
                    =foo[1] == 'spam'
                    >
                    For convenience it should also affect the range function so that:
                    >
                    range(3) = [1, 2, 3]
                    >
                    because this is often used where arrays would be used in VB.
                    >
                    Finally, when the programmer does not specify his choice of base at
                    the beginning of the program, the current behaviour of using 0 as base
                    should continue so that there is no problem with backward
                    compatibility.
                    Here is a document giving good reasons for indexing to start at
                    zero, as in Python.

                    The author has done a bit:


                    Having more than one index start point would be a maintenance
                    nightmare best avoided. (It can be done in Perl).

                    - Paddy.

                    Comment

                    • Sherm Pendley

                      #11
                      Re: Python Feature Request: Allow changing base of member indices to 1

                      "Paddy" <paddy3118@goog lemail.comwrite s:
                      Having more than one index start point would be a maintenance
                      nightmare best avoided.
                      Quite right.
                      (It can be done in Perl).
                      When was the last time you used Perl? It was allowed in Perl 4 and earlier,
                      because many Perl users were moving from Awk, which uses an array base of 1.
                      Even then, having multiple index start points within a single program wasn't
                      the idea; the idea was to allow programs originally written in Awk to be
                      ported to Perl with minimal updating.

                      It was deprecated as of 5.0 though - which was released in '94. It still
                      works, for the sake of backwards compatibility, but its use in new code is
                      highly discouraged.

                      sherm--

                      --
                      Web Hosting by West Virginians, for West Virginians: http://wv-www.net
                      Cocoa programming in Perl: http://camelbones.sourceforge.net

                      Comment

                      • John Machin

                        #12
                        Re: Python Feature Request: Allow changing base of member indices to 1

                        On Apr 14, 8:27 pm, samj...@gmail.c om wrote:
                        This is like the previous one. Please check for sanity and approve for
                        posting at python-dev.
                        >
                        I would like to have something like "option base" in Visual Basic.
                        IIRC it used to allow me to choose whether 0 or 1 should be used as
                        the base of member indices of arrays. In Python, the same can be used
                        with strings, lists, tuples etc.
                        >
                        This would mean:
                        foo = "foo"
                        =foo[1] == 'f'
                        >
                        foo = ['foo', 'bar', 'spam' ]
                        =foo[1] == 'foo'
                        >
                        foo = ('spam', 'eggs')
                        =foo[1] == 'spam'
                        >
                        For convenience it should also affect the range function so that:
                        >
                        range(3) = [1, 2, 3]
                        >
                        because this is often used where arrays would be used in VB.
                        >
                        Finally, when the programmer does not specify his choice of base at
                        the beginning of the program, the current behaviour of using 0 as base
                        should continue so that there is no problem with backward
                        compatibility.
                        +1 F***ed Concept of the Year


                        Comment

                        • Paddy

                          #13
                          Re: Python Feature Request: Allow changing base of member indices to 1

                          On Apr 14, 8:55 pm, Sherm Pendley <spamt...@dot-app.orgwrote:
                          "Paddy" <paddy3...@goog lemail.comwrite s:
                          Having more than one index start point would be a maintenance
                          nightmare best avoided.
                          >
                          Quite right.
                          >
                          (It can be done in Perl).
                          >
                          When was the last time you used Perl? It was allowed in Perl 4 and earlier,
                          because many Perl users were moving from Awk, which uses an array base of 1.
                          Even then, having multiple index start points within a single program wasn't
                          the idea; the idea was to allow programs originally written in Awk to be
                          ported to Perl with minimal updating.
                          >
                          It was deprecated as of 5.0 though - which was released in '94. It still
                          works, for the sake of backwards compatibility, but its use in new code is
                          highly discouraged.
                          >
                          sherm--
                          >
                          --
                          Web Hosting by West Virginians, for West Virginians:http://wv-www.net
                          Cocoa programming in Perl:http://camelbones.sourceforge.net
                          I use both Perl and Awk regularly. I did at one stage use the Awk-to-
                          Perl
                          translator as I learned Awk before Perl, but gave it up as the
                          generated
                          Perl was hard to extend, and I learned more Perl.

                          So Perl had it, and now has it deprecated. Python does not have it,
                          but
                          the original poster wants it. I don't think we should add it to Python
                          because it would make porting VB code easier.

                          - Paddy.

                          Comment

                          • Bjoern Schliessmann

                            #14
                            Re: Python Feature Request: Allow changing base of member indices to 1

                            jamadagni wrote:
                            OK fine. It is clear that this feature must be implemented if at
                            all only on a per-module basis. So can we have votes for
                            per-module implementation of this feature?
                            I don't think it's worth the hassle. BTW, what's, IYHO, the distinct
                            advantage of starting array indices at 1?

                            Regards,


                            Björn

                            --
                            BOFH excuse #90:

                            Budget cuts

                            Comment

                            • Sherm Pendley

                              #15
                              Re: Python Feature Request: Allow changing base of member indices to 1

                              "Paddy" <paddy3118@goog lemail.comwrite s:
                              I don't think we should add it to Python
                              because it would make porting VB code easier.
                              Great Cthulhu no!

                              I chimed in because your first comment regarding Perl implied that it's
                              commonplace for Perl programmers to fiddle with the index base. It can
                              be done, for historical reasons, but it's far from common.

                              I have to wonder, if the OP wants VB so badly, why is he using Python to
                              begin with?

                              sherm--

                              --
                              Web Hosting by West Virginians, for West Virginians: http://wv-www.net
                              Cocoa programming in Perl: http://camelbones.sourceforge.net

                              Comment

                              Working...