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

2005-11-06 Thread Alan Gauld
> 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 
directly.

HTH,

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


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


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

2005-11-06 Thread Kent Johnson
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
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

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


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

2005-11-06 Thread Trent Rigsbee
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!


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor