首页 > 其他 > 详细

Tuple assignment

时间:2014-07-19 18:31:53      阅读:362      评论:0      收藏:0      [点我收藏+]

It is often useful to swap the values of two variables. With conventional assignments, you have to use a temporary variable. This solution is cumbersome; tuple assignment is more elegant:

 bubuko.com,布布扣                      

The left side is a tuple of variables; the right side is a tuple of expressions. Each value is assigned to its respective variable. All the expressions on the right side are evaluated before any of the assignments. The number of variables on the left and the number of values on the right have to be the same:

 bubuko.com,布布扣

More generally, the right side can be any kind of sequence (string, list or tuple). For example, to split an email address into a user name and a domain, you could write:

 bubuko.com,布布扣

 

from Thinking in Python

Tuple assignment,布布扣,bubuko.com

Tuple assignment

原文:http://www.cnblogs.com/ryansunyu/p/3854983.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!