Python在一行上分配两个变量 [英] Python assigning two variables on one line
本文介绍了Python在一行上分配两个变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
class Domin():
def __init__(self , a, b) :
self.a=a , self.b=b
def where(self):
print 'face : ' , self.a , "face : " ,self.b
def value(self):
print self.a + self.b
d1=Domin(1 , 5)
d1=Domin(20 , 15)
我收到此错误:
Traceback (most recent call last):
File "test2.py", line 13, in <module>
d1=Domin(1 , 5)
File "test2.py", line 5, in __init__
self.a=a , self.b=b
TypeError: 'int' object is not iterable
推荐答案
你不能把两个语句就像那样。您的代码正在评估如下:
You cannot put two statements on one line like that. Your code is being evaluated like this:
self.a = (a, self.b) = b
使用分号(第二个想法,不要那样做):
Either use a semicolon (on second thought, don't do that):
self.a = a; self.b = b
或者使用序列解包:
self.a, self.b = a, b
或者只是把它分成两行:
Or just split it into two lines:
self.a = a
self.b = b
我会这样做。
这篇关于Python在一行上分配两个变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文