lundi 20 juin 2016

If statement ordering and key error python

Here I get a key error even I check if the key exists in the dict :

def foo(d):
    if (('element' in d.keys()) & (d['element'] == 1)):
        print "OK"

foo({})

In the documentation we can read :

The expression x and y first evaluates x; if x is false, its value is returned; otherwise, y is evaluated and the resulting value is returned.

Can anybody explain me this behavior?

Aucun commentaire:

Enregistrer un commentaire