[英]Class function not returning the correct value
我有這門課:
class test(object):
def __init__(self, s):
self.s = s
def getS(self):
return self.s
我需要生成此類的5個實例,並且只有1個實例將具有值等於true的變量s。
tests = [test(True) if i==sys.argv[1] else test(False) for i in range(5)]
然后我試着打印這些值。
for i in tests:
print i.getS()
輸出是:
False
False
False
False
False
其中一個值不應該等於True嗎?
試試這個,它有點簡單。 另外,如果使用Python 2.x,則使用xrange
如果使用Python 3.x,則使用range
。
tests = [test(i == int(sys.argv[1])) for i in xrange(5)]
你的代碼本質上是正確的 - 你只是忘了轉換為int
命令行參數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.