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

Alan Gauld alan.gauld at freenet.co.uk
Sun Nov 6 20:43:09 CET 2005

> 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!

It looks like you may be reading the wrong book!
Why they would suggest you ever execute that code I have no idea,
using the has_key() method is the normal way to do that.

However, if you really want to call contains() it has two underscores on 
each side.
This is a standard Python idiom for naming functions which you are *not*
expected to call directly. Occasionally you do need to do so but never
as a beginner! Hence why is "Beginning Python" even telling you about them?

The purpose of the  __contains__ () function is to allow you to modify the
behaviour of the has_key() method (which does call __contains__() ) should
you define your own dictionary type, you should not have to call it 


Alan G
Author of the learn to program web tutor

More information about the Tutor mailing list