'dict' object has no attribute 'has_key' in Python3

3

In Python 2, when I wanted to know if a dict had a given key, I used the has_key method.

if kwargs.has_key("code"):
   self.code = kwargs.code

However, now that I was running the same script in Python 3, I got the following error:

  

AttributeError: 'dict' object has no attribute 'has_key'

In Python 3, has this has_key method been removed? Which option do I have now?

    
asked by anonymous 04.11.2016 / 14:01

1 answer

5

The method dict.has_key has been removed in Python 3.x , use the in operator.

  

Removed. dict.has_key() - use the in operator instead.

In your case, you can use it like this:

if "code" in kwargs:
   self.code = kwargs.code
    
04.11.2016 / 14:04