import pygame
from pygame.locals import *
from sys import exit
from random import randint

pygame.init()

screen = pygame.display.set_mode((640, 480), 0, 32)

while True:
    
    for event in pygame.event.get():
        if event.type == QUIT:
            exit()
            
        rand_col = (randint(0, 255), randint(0, 255), randint(0, 255))
        
        for _ in xrange(100):
            rand_pos = (randint(0, 630), randint(0, 479))
            screen.set_at(rand_pos, rand_col)
            
        pygame.display.update()

In the above code, what on earth is the little underscore doing in that for loop?
for _

Also, what does xrange do? I'M pretty sure that's a simple one but I can't remember. Thanks.

It is simply variable name. I use it when I want to write
"put in this variable the value, which I do not need"

The for is expression to say "Do this 100 times"

_ has other use, which has nothing to do the script you posted: _ variable in command line however is used to store value of last executed command:

Python 2.6.5 (r265:79096, Mar 19 2010, 21:48:26) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> 2+3
5
>>> _
5
>>>

xrange is more limited range command, which is slightly faster, but going to be discontinued in future:

Python 3.1.2 (r312:79149, Mar 21 2010, 00:41:52) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>> 
Traceback (most recent call last):
  File "C:/temp/xr.py", line 1, in <module>
    print(xrange(10))
NameError: name 'xrange' is not defined
>>>

Edited 6 Years Ago by pyTony: n/a

It's not exactly true. xrange() is a builtin function in python 2 which returns an iterator instead of a list (range() returns a list). In python 3, range() returns an iterator, so python 3's range is python 2's xrange. In python 2, use xrange rather than range, because it's faster.

Also Garrett85, when you don't know what a builtin symbol does, go to this page http://docs.python.org/, and write the symbol in the box 'Quick Search' on the left. It's very efficient.

Edited 6 Years Ago by Gribouillis: n/a

Sorry Gribouillis, but xrange is not generator in Python 2, but one special limited type:

Python 2.7 (r27:82525, Jul  4 2010, 09:01:59) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> a=xrange(10)
>>> a.next()

Traceback (most recent call last):
  File "<pyshell#3>", line 1, in <module>
    a.next()
AttributeError: 'xrange' object has no attribute 'next'
>>> a=iter(range(10))
>>> a
<listiterator object at 0x00EA66D0>
>>> a.next()
0
>>> a.next()
1
>>> a=iter(xrange(10))
>>> a
<rangeiterator object at 0x00EA2A88>
>>> a.next()
0
>>> next(a)
1

5.6.3. XRange Type¶

The xrange type is an immutable sequence which is commonly used for looping. The advantage of the xrange type is that an xrange object will always take the same amount of memory, no matter the size of the range it represents. There are no consistent performance advantages.

XRange objects have very little behavior: they only support indexing, iteration, and the len() function.

Edited 6 Years Ago by pyTony: n/a

Sorry, but xrange was introduced in python when the iteration protocol was defined. The purpose of xrange is to yield numbers from start to stop without building explicitly a list. This was considered an improvement over range (which goes back to the very early versions of python). The fact that xrange is a type is an implementation detail: the generator must store at least an integer to yield the next value.

Edited 6 Years Ago by Gribouillis: n/a

Thanks for informing us, I tried to experiment little but with not so much results:

Traceback (most recent call last):
  File "D:/test/xr.py", line 1, in <module>
    class x2range(xrange):
TypeError: Error when calling the metaclass bases
    type 'xrange' is not an acceptable base type

In a loop, the iter method is called: see this experiment

>>> x = xrange(10)
>>> x
xrange(10)
>>> y = iter(x)
>>> y
<rangeiterator object at 0x7fcbaaff2ed0>
>>> y.next
<method-wrapper 'next' of rangeiterator object at 0x7fcbaaff2ed0>

...
In the above code, what on earth is the little underscore doing in that for loop?
for _
...

The underscore is a variable name Python uses internally. Someone is trying to be a show-off! IMHO, those things lead to unnecessary confusion.

BTW, where are you copying this dated pygame code from?

Edited 6 Years Ago by vegaseat: dated

This question has already been answered. Start a new discussion instead.