gsamaras gsamaras - 2 years ago 97
Python Question

NameError: global name '_' is not defined

The code:

if (query_id, _) in hashtable[bucket]:

I expected this to work like in a for loop, but instead it gives this error:

NameError: global name '_' is not defined

is a list of pairs if that matters (which I doubt). Any ideas?

Answer Source

The x in y is not magic; it's basically the same as y.__contains__(x). Therefore, in cannot search with placeholders; the left argument is fully evaluated. Instead, use

if any(query_id == qid for (qid, _) in hashtable[bucket]):
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download