码迷,mamicode.com
首页 > 其他好文 > 详细

Tuple assignment

时间:2014-07-19 18:32:06      阅读:243      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   2014   io   for   

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

标签:style   blog   http   2014   io   for   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!