Syntax __contains__

Ive written a python program that used multiple instances of the syntax contains to look inside dictionaries and lists.

if not test_map[var].__contains__(string): 

It seems I would be better off using:

 if string not in test_map[var]: 

Does anyone know what problems might arise, is the __contains__ method __contains__ ?

Thanks,

+4
source share
1 answer

This is very ugly.

Using the implementation directly instead of using the supposed top-level syntax can also hurt you if the semantics are changed in the future. In addition, it makes the code much more difficult to understand and explain.

It is possible to be a competent Python programmer without knowing that in uses __contains__() under the hood. The converse is what I would say is wrong.

+12
source

Source: https://habr.com/ru/post/1438396/


All Articles