what does 'for _ in range()' mean?

Discussion in 'Python' started by Jon Perez, Jul 28, 2004.

  1. Jon Perez

    Jon Perez Guest

    I saw this code snippet:

    sock.listen(20)
    for _ in range(20):
    newsock, client_addr = sock.accept()
    print "Client connected:", client_addr
    data[newsock] = ""

    why use _ for this example? Is there any
    optimization to be had using it?

    I know that in the interpreter _ means the
    last value calculated, but what does _ mean
    inside source code?
     
    Jon Perez, Jul 28, 2004
    #1
    1. Advertising

  2. In article <>,
    Jon Perez <> wrote:

    > I saw this code snippet:
    >
    > sock.listen(20)
    > for _ in range(20):
    > newsock, client_addr = sock.accept()
    > print "Client connected:", client_addr
    > data[newsock] = ""
    >
    > why use _ for this example? Is there any
    > optimization to be had using it?
    >
    > I know that in the interpreter _ means the
    > last value calculated, but what does _ mean
    > inside source code?


    AFAIK it's just a variable like any other, but by convention it means
    that you don't intend to use that value, just read it and ignore it.

    --
    David Eppstein
    Computer Science Dept., Univ. of California, Irvine
    http://www.ics.uci.edu/~eppstein/
     
    David Eppstein, Jul 28, 2004
    #2
    1. Advertising

  3. Jon Perez

    Roy Smith Guest

    In article <>,
    David Eppstein <> wrote:

    > In article <>,
    > Jon Perez <> wrote:
    >
    > > I saw this code snippet:
    > >
    > > sock.listen(20)
    > > for _ in range(20):
    > > newsock, client_addr = sock.accept()
    > > print "Client connected:", client_addr
    > > data[newsock] = ""
    > >
    > > why use _ for this example? Is there any
    > > optimization to be had using it?
    > >
    > > I know that in the interpreter _ means the
    > > last value calculated, but what does _ mean
    > > inside source code?

    >
    > AFAIK it's just a variable like any other, but by convention it means
    > that you don't intend to use that value, just read it and ignore it.


    I've never heard of that convention before. Is it some python-specific
    thing, or is my ignorance more global in scope?

    In any case, I'd vote for some more useful variable name. In the above
    case, something like connectionNumber or whatever would be more
    self-explanitory.
     
    Roy Smith, Jul 28, 2004
    #3
  4. Roy Smith <> writes:

    > I've never heard of that convention before. Is it some python-specific
    > thing, or is my ignorance more global in scope?


    It may derive from Prolog, where you use _ as an unbound "wildcard".
    E.g. to declare that X is a parent if X is a father or mother:

    parent(X) :- fatherOf(X, _), !.
    parent(X) :- motherOf(X, _), !.

    because you don't need to know anything about the child, just that it
    exists. If you used a variable like Y, you would either also need to
    say something about Y, or Y would end up in the resolved answer.

    (The ! is a "cut", and means we're not interested in more than one
    answer.)
     
    Tor Iver Wilhelmsen, Jul 28, 2004
    #4
  5. Tor Iver Wilhelmsen wrote:
    >
    > Roy Smith <> writes:
    >
    > > I've never heard of that convention before. Is it some python-specific
    > > thing, or is my ignorance more global in scope?

    >
    > It may derive from Prolog, where you use _ as an unbound "wildcard".
    > E.g. to declare that X is a parent if X is a father or mother:


    It may, but to answer the original poster's question, it's not uncommon
    to see this in other languages (where _ is a valid identifier). _ often
    means a variable one's not interested in the value of, but which is
    needed for syntactic reasons.

    Note that _(...) as a macro call has another conventional meaning, which
    comes from gettext, where one uses _("string literal") to indicate a
    string that needs localization.

    Note also there's a noticeable difference between the anonymous variable
    in Prolog and the use of _ in Python; in Prolog, the anonymous variable
    can be used multiple times in the same expression and there is no need
    for the variable to represent the same thing. In

    middle(X) :- inOrder(_, X, _).

    there's no need for _ to map to the same object -- middle(c) would be
    true even if inOrder(b, c, d) were the only known relevant fact, and b
    and d are certainly not equal. That's not true in Python, where _ is
    just another name with no special semantics.

    --
    __ Erik Max Francis && && http://www.alcyone.com/max/
    / \ San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
    \__/ Never be the first to believe / Never be the last to deceive
    -- Florence, _Chess_
     
    Erik Max Francis, Jul 28, 2004
    #5
  6. Roy Smith wrote:

    > I've never heard of that convention before. Is it some python-specific
    > thing, or is my ignorance more global in scope?


    I have seen it in logic and functional programming, where '_' -
    differently from python - has a special meaning: assignments to '_' are
    discarded in functional programming and

    I guess that it's value in the interpreter, instead, comes from perl's $_.

    > In any case, I'd vote for some more useful variable name. In the above
    > case, something like connectionNumber or whatever would be more
    > self-explanitory.


    In that case, I interpret is as this: that loop has to be iterated 20
    times, and the looping variable is uninfluent. In this cases, again by
    convention, in C programs the variable is often called "i".

    --
    Ciao,
    Matteo
     
    Matteo Dell'Amico, Jul 28, 2004
    #6
  7. Whoops, part missing :)

    Matteo Dell'Amico wrote:

    > I have seen it in logic and functional programming, where '_' -
    > differently from python - has a special meaning: assignments to '_' are
    > discarded in functional programming and


    in logic programming it is seen as a jolly "match-all" value.

    --
    Ciao,
    Matteo
     
    Matteo Dell'Amico, Jul 28, 2004
    #7
  8. Jon Perez

    John Roth Guest

    "David Eppstein" <> wrote in message
    news:...
    > In article <>,
    > Jon Perez <> wrote:
    >
    > > I saw this code snippet:
    > >
    > > sock.listen(20)
    > > for _ in range(20):
    > > newsock, client_addr = sock.accept()
    > > print "Client connected:", client_addr
    > > data[newsock] = ""
    > >
    > > why use _ for this example? Is there any
    > > optimization to be had using it?
    > >
    > > I know that in the interpreter _ means the
    > > last value calculated, but what does _ mean
    > > inside source code?

    >
    > AFAIK it's just a variable like any other, but by convention it means
    > that you don't intend to use that value, just read it and ignore it.


    What convention? I have to agree with a couple
    of other posters; I've never heard of it before.

    If it really is a convention, it would be nice to have it
    documented somewhere (like the single underscore
    in front of a variable means "weak internal use").
    Somewhere is most likely PEP 8 - the Python
    Style Guide.

    John Roth

    >
    > --
    > David Eppstein
    > Computer Science Dept., Univ. of California, Irvine
    > http://www.ics.uci.edu/~eppstein/
     
    John Roth, Jul 28, 2004
    #8
  9. John Roth wrote:

    > What convention? I have to agree with a couple
    > of other posters; I've never heard of it before.
    >
    > If it really is a convention, it would be nice to have it
    > documented somewhere (like the single underscore
    > in front of a variable means "weak internal use").
    > Somewhere is most likely PEP 8 - the Python
    > Style Guide.


    A convention doesn't need to have official sanction to be a convention.

    --
    __ Erik Max Francis && && http://www.alcyone.com/max/
    / \ San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
    \__/ But you're not going to be there tomorrow. And it's all about
    tomorrow. -- Montgomery Brogan
     
    Erik Max Francis, Jul 28, 2004
    #9
  10. Jon Perez

    Peter Hansen Guest

    John Roth wrote:

    > "David Eppstein" <> wrote:
    >> Jon Perez <> wrote:
    >>>I know that in the interpreter _ means the
    >>>last value calculated, but what does _ mean
    >>>inside source code?

    >>
    >>AFAIK it's just a variable like any other, but by convention it means
    >>that you don't intend to use that value, just read it and ignore it.

    >
    > What convention? I have to agree with a couple
    > of other posters; I've never heard of it before.


    I would imagine it's the same convention that suggests that
    the value assigned to _ is ignored in the following type
    of code, which surely most of us have seen around here a
    few times:

    a, b, _ = tupleWithThreeItems

    -Peter
     
    Peter Hansen, Jul 28, 2004
    #10
  11. I must admit I've never heard of this before either but, I'm no world
    authority on Python.

    I've just tried it on some toy code and as always I run pychecker on it. So
    with

    def spam():
    for i in range(10):
    print 'a'

    I get "Local variable (i) not used". But with the following:

    def spam():
    for _ in range(10):
    print 'a'

    I get no such warning. Is pychecker aware of this convention or is it
    coincidental? I don't
    know.

    Michael Charlton



    "David Eppstein" <> wrote in message
    news:...
    > In article <>,
    > Jon Perez <> wrote:
    >
    > > I saw this code snippet:
    > >
    > > sock.listen(20)
    > > for _ in range(20):
    > > newsock, client_addr = sock.accept()
    > > print "Client connected:", client_addr
    > > data[newsock] = ""
    > >
    > > why use _ for this example? Is there any
    > > optimization to be had using it?
    > >
    > > I know that in the interpreter _ means the
    > > last value calculated, but what does _ mean
    > > inside source code?

    >
    > AFAIK it's just a variable like any other, but by convention it means
    > that you don't intend to use that value, just read it and ignore it.
    >
    > --
    > David Eppstein
    > Computer Science Dept., Univ. of California, Irvine
    > http://www.ics.uci.edu/~eppstein/
     
    Michael Charlton, Jul 28, 2004
    #11
  12. Jon Perez

    Peter Otten Guest

    Michael Charlton wrote:

    > I've just tried it on some toy code and as always I run pychecker on it.
    > So with
    >
    > def spam():
    > for i in range(10):
    > print 'a'
    >
    > I get "Local variable (i) not used". But with the following:
    >
    > def spam():
    > for _ in range(10):
    > print 'a'
    >
    > I get no such warning. Is pychecker aware of this convention or is it
    > coincidental? I don't


    Found in Config.py:

    _DEFAULT_UNUSED_LIST = [ '_', 'empty', 'unused', 'dummy', ]

    Peter
     
    Peter Otten, Jul 28, 2004
    #12

  13. >> Is pychecker aware of this convention ...


    Peter> Found in Config.py:

    Peter> _DEFAULT_UNUSED_LIST = [ '_', 'empty', 'unused', 'dummy', ]

    One might consider adding 'i' to that list. If nothing else, it would avoid
    a bunch of warnings... ;-)

    Skip
     
    Skip Montanaro, Jul 28, 2004
    #13
  14. On Wed, 28 Jul 2004, Skip Montanaro wrote:

    > Peter> _DEFAULT_UNUSED_LIST = [ '_', 'empty', 'unused', 'dummy', ]
    >
    > One might consider adding 'i' to that list. If nothing else, it would avoid
    > a bunch of warnings... ;-)


    But then we'd have to add a, t, n, and x, too ;)
     
    Christopher T King, Jul 28, 2004
    #14
  15. Jon Perez

    Peter Otten Guest

    Skip Montanaro wrote:

    >
    > >> Is pychecker aware of this convention ...

    >
    > Peter> Found in Config.py:
    >
    > Peter> _DEFAULT_UNUSED_LIST = [ '_', 'empty', 'unused', 'dummy', ]
    >
    > One might consider adding 'i' to that list. If nothing else, it would
    > avoid
    > a bunch of warnings... ;-)


    All but "_" work as prefix, too, so that could avoid a bit too much...

    Peter
     
    Peter Otten, Jul 28, 2004
    #15
  16. Jon Perez

    Terry Reedy Guest

    > What convention? I have to agree with a couple
    > of other posters; I've never heard of it before.


    Coming from Fortran and C, I myself use i for 'don't care' looping vars.

    > If it really is a convention,


    It is obviously not a universal convention. What does library code use?

    > it would be nice to have it
    > documented somewhere (like the single underscore
    > in front of a variable means "weak internal use").
    > Somewhere is most likely PEP 8 - the Python
    > Style Guide.


    In the interactive interpreter, _ is bound to the last expression evaluated
    that is otherwise unassigned.

    >>> a=3
    >>> _

    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    NameError: name '_' is not defined
    >>> 3

    3
    >>> _

    3

    This is the only official use of _ that I know of and it is documented
    somewhere.

    Terry J. Reedy
     
    Terry Reedy, Jul 28, 2004
    #16
  17. Jon Perez

    Phil Frost Guest

    At Some Point, Someone Asked Something to The Effect of:
    > where does this _ thing come from?


    In ML, _ is used as magic "don't care" variable. It's often used when
    unpacking tuples and some elements are irrelevant. Example:

    Ocaml:
    let (fu, bar, _, _) = returns_a_4_tuple ()

    Python:
    (fu, bar, _, __) = returns_a_4_tuple()

    One difference is that _ is allowed multiple times in Ocaml, while in
    Python it is not. Also to Python, it's a variable like any other, while
    in Ocaml it's magic.
     
    Phil Frost, Jul 28, 2004
    #17
  18. Jon Perez

    Peter Hansen Guest

    Phil Frost wrote:

    > Python:
    > (fu, bar, _, __) = returns_a_4_tuple()
    >
    > One difference is that _ is allowed multiple times in Ocaml, while in
    > Python it is not.


    Why do you say that?

    c:\>python
    Python 2.3.4 (#53, May 25 2004, 21:17:02) ....
    >>> a = 1, 2, 3, 4
    >>> b, c, d, e = a
    >>> b, c, d, d = a
    >>> b, c, _, _ = a
    >>> _, _, _, _ = a
    >>> _

    4

    Looks to me like Python doesn't care how many times it rebinds
    a name while unpacking a tuple.

    -Peter
     
    Peter Hansen, Jul 28, 2004
    #18
  19. Christopher T King wrote:
    > On Wed, 28 Jul 2004, Skip Montanaro wrote:
    >
    >>One might consider adding 'i' to that list. If nothing else, it would avoid
    >>a bunch of warnings... ;-)

    >
    > But then we'd have to add a, t, n, and x, too ;)


    Don't forget j.
     
    Leif K-Brooks, Jul 28, 2004
    #19
  20. In article <>,
    Peter Hansen <> wrote:

    > Phil Frost wrote:
    > > One difference is that _ is allowed multiple times in Ocaml, while in
    > > Python it is not.

    > Why do you say that?
    >
    > c:\>python
    > Python 2.3.4 (#53, May 25 2004, 21:17:02) ....
    > >>> a = 1, 2, 3, 4
    > >>> b, c, d, e = a
    > >>> b, c, d, d = a
    > >>> b, c, _, _ = a
    > >>> _, _, _, _ = a
    > >>> _

    > 4


    In that context, it's allowed multiple times, but in some other contexts
    it's not:

    >>> def foo(_,_): pass

    ....
    File "<stdin>", line 1
    SyntaxError: duplicate argument '_' in function definition
    >>>


    --
    David Eppstein
    Computer Science Dept., Univ. of California, Irvine
    http://www.ics.uci.edu/~eppstein/
     
    David Eppstein, Jul 28, 2004
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    46
    Views:
    1,000
    Antoon Pardon
    Jul 25, 2006
  2. Li Ma
    Replies:
    1
    Views:
    2,368
    Roedy Green
    Mar 9, 2009
  3. Rahul
    Replies:
    4
    Views:
    627
    Robert Kern
    Apr 7, 2009
  4. C Barrington-Leigh
    Replies:
    1
    Views:
    1,286
    Tim Leslie
    Sep 10, 2010
  5. Tomoyuki Kosimizu

    Range does not take an Range object.

    Tomoyuki Kosimizu, Nov 25, 2003, in forum: Ruby
    Replies:
    3
    Views:
    171
    Tomoyuki Kosimizu
    Nov 27, 2003
Loading...

Share This Page