Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

__contains__ syntax

Tags:

python

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

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

It seems that I would of been better to use :

if string not in test_map[var]:

Does anyone know the issues that can occur is using the __contains__ method ?

Thanks,

like image 276
felix001 Avatar asked Oct 08 '12 11:10

felix001


1 Answers

It's very very ugly.

Using the implementation directly instead of using the actual intended top-level syntax can also hurt you if the semantics are changed in the future. Plus, it makes the code much harder to comprehend and explain.

It's quite possible to be a competent Python programmer without knowing that in uses __contains__() under the hood. The reverse I would claim is not true.

like image 75
unwind Avatar answered Sep 27 '22 22:09

unwind