Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does pix[x, y] mean in Python

I find a strange statement when I'm reading PIL document.

In 1.1.6 and later, load returns a pixel access object that can be used to read and modify pixels. The access object behaves like a 2-dimensional array, so you can do:

pix = im.load()
print pix[x, y]
pix[x, y] = value

What does pix[x, y] mean here? It's not slicing syntax because , used rather than :.

like image 436
Clippit Avatar asked Nov 10 '12 14:11

Clippit


People also ask

What does XY mean in Python?

It means that the function you have called returns an iterable, and the index 0 of the iterable is assigned to x and the index 1 is assigned to y. This is called tuple unpacking .


1 Answers

pix[x, y]

is the same as

t = x, y
pix[t]

or

t = (x, y)
pix[t]

or

pix[(x, y)]

What you're seeing is a tuple literal inside an item-getting expression, in the same way that I can nest other expressions such as l[1 if skip else 0]

like image 52
Eric Avatar answered Sep 21 '22 23:09

Eric