[Tutor] Dictionary Error: 'dict' object has no attribute '_contains_'

Kent Johnson kent37 at tds.net
Sun Nov 6 18:47:21 CET 2005

Trent Rigsbee wrote:
> Hi! I'm on the version 2.4, going through Beginning Python (Wrox),  and I'm 
> getting the above error. I'm trying to do this:
> menu_specials._contains_("test")
> Any ideas on what I'm doing wrong? Thanks!

The name of the method is __contains__ (note *double* leading and trailing underscores). But you normally shouldn't call this directly, you should write
  "test" in menu_specials

In general the methods and atttributes whose names start and end with double underscores are special to the language (in fact they are called 'special methods') and they are rarely called by code you write. They are hooks that let the author of a class define its behaviour. In this case, the __contains__() method is created to define how a dict object tests for membership.



More information about the Tutor mailing list