Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
408 views
in Technique[技术] by (71.8m points)

int - Python assigning two variables on one line

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)

I get this error:

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

Or use sequence unpacking:

self.a, self.b = a, b

Or just split it into two lines:

self.a = a
self.b = b

I would do it the last way.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...