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

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -