在Python中了解一行中的多个变量分配 [英] Understanding multiple variable assignment on one line in Python

查看:81
本文介绍了在Python中了解一行中的多个变量分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定如何正确地问这个问题,因为我试图了解一些我尚不了解的内容,因此我不知道要使用的正确术语,但是我在有关PIL的YouTube教程,此处.

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.

问题

有人可以解释一下最后一行的意思吗?我猜测这是一种Python风格的声明变量,我尚不熟悉,而且我不知道它叫什么,所以我无法查找它.

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

我尝试过的事情

我试图打破最后一行的逻辑,但这对我来说没有意义:

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 

我看到的问题是:

    未定义
  • width.

image.size表示图像尺寸,即(512,512),因此似乎不太适合height.

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.

推荐答案

解决方案

我想我是通过运行以下命令来解决这个问题的:

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

意思是:

变量size是一个由两个值(widthheight)组成的元组,它们的值分别是512512 resp".

"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.

这篇关于在Python中了解一行中的多个变量分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆