Understanding multiple variable assignment on one

2019-05-10 06:43发布

I'm not sure how to ask this question properly cause I'm trying to understand something I don't understand yet and therefore I don't know the right terminology to use, but I came across this code snippet in a YouTube tutorial regarding PIL here.

Question

Could somebody please explain what the last line means? I'm guessing it is a Python style of declaring variables I am not familiar with yet and I dont know what it is called so I can't look it up.

import Image

filename = "py.png"
image = Image.open(filename)
size = width, height = image.size

What I've Tried

I've tried to break down the logic of the last line but it doesn't make sense to me:

# assign value of width to size variable?     
size = width 
# assign value of image.size to height variable?
height = image.size 

The problems I see with this are:

  • width is not defined.

  • image.size represents the images dimensions ie (512,512) so that doesn't seem like it would be an appropriate value for height.

I'm sure this is very simple, I just don't get it yet.

1条回答
SAY GOODBYE
2楼-- · 2019-05-10 07:27

Solution

I think I figured it out by running the following:

>>> size = width, height = 320, 240;
>>> size
(320, 240)
>>> width
320
>>> height
240

So essentially what the code in the original post is saying is:

>>> size = width, height = 512, 512;
>>> size
(512, 512)
>>> width
512
>>> height
512

Which means:

"The variable size is a tuple made of two values (width and height) and their values are 512 and 512 resp".

The mechanics of it haven't really sunk in yet, but good enough for now.

查看更多
登录 后发表回答