Suggestion for the PythonDevelopment for next version

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • azrael

    Suggestion for the PythonDevelopment for next version

    You know, sometimes it annoys me to write a for loop in Python. If we
    use a list a=[1,2,3,4], and want to loop through it, Python offers the
    next option
    >>>for i in a:
    >> print i
    >>>
    1
    2
    3
    4

    I love this. So simple and smooth. But what happens if we need also
    the position of an object in a list. Then there comes this annoying
    writing.
    >>for i in range(len(a)):
    >> print a[i], i
    >>>
    1 0
    2 1
    3 2
    4 3

    I think that it would be great if the Python language, which is a
    totaly OOP language, could also use the index variable from the first
    example and consider it as an object with a Object variable. I mean
    the following.
    >>>for i in a:
    >> print i, i.index # i.index is my sugesstion
    >>>
    1 0
    2 1
    3 2
    4 3

    I think that this would be great and we cou pass by this annoying
    "range(len( a))" functions
  • Diez B. Roggisch

    #2
    Re: Suggestion for the PythonDevelopme nt for next version

    azrael wrote:
    You know, sometimes it annoys me to write a for loop in Python. If we
    use a list a=[1,2,3,4], and want to loop through it, Python offers the
    next option
    >>>>for i in a:
    >>> print i
    >>>>
    1
    2
    3
    4
    >
    I love this. So simple and smooth. But what happens if we need also
    the position of an object in a list. Then there comes this annoying
    writing.
    >
    >>>for i in range(len(a)):
    >>> print a[i], i
    >>>>
    1 0
    2 1
    3 2
    4 3
    >
    I think that it would be great if the Python language, which is a
    totaly OOP language, could also use the index variable from the first
    example and consider it as an object with a Object variable. I mean
    the following.
    >
    >>>>for i in a:
    >>> print i, i.index # i.index is my sugesstion
    >>>>
    1 0
    2 1
    3 2
    4 3
    >
    I think that this would be great and we cou pass by this annoying
    "range(len( a))" functions
    Luckily for those who read either the documentation or this form regulary,
    the solution is already there (since python2.0 or so...)

    for i, item in enumerate(itera ble):
    ...

    does the trick.

    Diez

    Comment

    • Bruno Desthuilliers

      #3
      Re: Suggestion for s/the PythonDevelopme nt for next version/azrael

      azrael a écrit :
      You know, sometimes it annoys me to write a for loop in Python. If we
      use a list a=[1,2,3,4], and want to loop through it, Python offers the
      next option
      >>>for i in a:
      >>> print i
      >>>>
      1
      2
      3
      4
      >
      I love this. So simple and smooth. But what happens if we need also
      the position of an object in a list. Then there comes this annoying
      writing.
      >
      >>>for i in range(len(a)):
      >>> print a[i], i
      >>>>
      1 0
      2 1
      3 2
      4 3

      for index, obj in enumerate(any_s equence):
      print "obj %s is at position %s" % (obj, index)

      print "And I might read the FineManual(tm) some day..."

      HTH !-)


      Oh, and while we're at it:
      I think that it would be great if the Python language, which is a
      totaly OOP language, could also use the index variable from the first
      example and consider it as an object with a Object variable. I mean
      the following.
      >
      >>>>>for i in a:
      >>>> print i, i.index # i.index is my sugesstion
      Python is 100% OO in that everything (at least everything you can bind
      to a name) is an object. But it doesn't mean everything has to be done
      using the dotted syntax...

      Comment

      Working...