5
In Python 2, when I wanted to know if a dict
had certain key, I used the method has_key
.
if kwargs.has_key("code"):
self.code = kwargs.code
However, now that I ran the same script in Python 3, I got the following error:
Attributeerror: 'Dict' Object has no attribute 'has_key'
In Python 3, this method has_key
has been removed? What option do I have now?
Cool. Apparently, this gives more performance, since instead of a method will use an operator..
– Wallace Maxters
Performance is exactly the same- operators in Python usually call internal methods. (in this case, the method
__contains__
) - but the rewrite is to use the syntax within
even in Python 2.– jsbueno
@jsbueno thank you so much for the information. I didn’t know it was possible to use
in
in Python 2. I always thought that "the right one" was thehas_key
.– Wallace Maxters