python - Wired behavior evaluating tuple -


when have list of tuples , try evaluate type each of elements of list in function see behavior

in [348]:def f2(x):              if x[0]==tuple:                  return true              else:                  return false   in [349]:w=[(0,1)]  in [350]:f2(w) out[350]: false 

but when evaluate elements indvidually expected result

in [351]:type(w[0])==tuple out[351]: true 

you forgot call type in if condition:

def f2(x):     if type(x[0])==tuple:         return true     else:         return false 

note, however, since each branch of if-else returns boolean, can remove , return condition's evaluation:

def f2(x):    return type(x[0])==tuple 

Comments

Popular posts from this blog

c# - Class in a list -

mysql - Django database model for my website -

ios - How to run a segue on itself OR another viewController -