2014-01-10 74 views
0

如何比较语句中中函数返回的元组的元素? 例如,我愿做类似下面的...如何比较if语句中函数的元组返回值

if platform.machine() == "AMD64" : 

此功能只有一个字符串varaible回报。除了使用platform.architecture(),它的返回值看起来像('32bit','WindowsPE'),我想要做同样的事情。我目前所做的是...

architecture = platform.architecture() 
if architecture[0] == "64bit": 

我想知道是否有更多pythonic可以在一行中实现。

+4

只是'if platform.machine()[0] =='64bit':'没有什么错误。 – RemcoGerlich

+0

@RemcoGerlich:请张贴这个答案,因为它是pythonic之一。 –

回答

2

因为,platform.architecture is unreliable,以获取处理器架构的最好的办法是

if platform.machine()[:-2] == "64": 
    # 64 bit machine 
else: 
    # 32 bit machine 

如果你真正需要的使用你从一个函数来获取价值的最佳途径,你可以简单地忽略其他值,并得到您需要的值使用索引

def temp(): 
    return 1, "Welcome" 
if temp()[1] == "Welcome": 
    print 1