Discussion:
[Tutor] Dictionary Error: 'dict' object has no attribute '_contains_'
Trent Rigsbee
2005-11-06 15:56:34 UTC
Permalink
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!
Kent Johnson
2005-11-06 17:47:21 UTC
Permalink
Post by Trent Rigsbee
Hi! I'm on the version 2.4, going through Beginning Python (Wrox), and I'm
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
instead.

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.

Kent
--
http://www.kentsjohnson.com
Alan Gauld
2005-11-06 19:50:40 UTC
Permalink
Post by Kent Johnson
But you normally shouldn't call this directly, you should write
"test" in menu_specials
Now howzabout that! Yet another new trick learnt.
When did 'in' start working with dictionaries?

Alan G.
Kent Johnson
2005-11-06 20:05:55 UTC
Permalink
Post by Alan Gauld
Post by Kent Johnson
But you normally shouldn't call this directly, you should write
"test" in menu_specials
Now howzabout that! Yet another new trick learnt.
When did 'in' start working with dictionaries?
Oh, about four years ago :-) in Python 2.2

You really should read the "What's New in Python x.x" docs ;)

Kent
--
http://www.kentsjohnson.com
Alan Gauld
2005-11-06 22:30:56 UTC
Permalink
Post by Kent Johnson
Oh, about four years ago :-) in Python 2.2
You really should read the "What's New in Python x.x" docs ;)
:-)

Hmm, I usually skim them but pick up most of my info from this
list because I rarely upgrade to the latest version right away
- I only upgraded my XP box. to 2.4 a few weeks ago...

Alan G.

Alan Gauld
2005-11-06 19:43:09 UTC
Permalink
Post by Trent Rigsbee
Hi! I'm on the version 2.4, going through Beginning Python (Wrox), and
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
directly.

HTH,

Alan G
Author of the learn to program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld
Loading...