[Tutor] Classes and has_key

Alan Gauld alan.gauld at blueyonder.co.uk
Thu Apr 15 03:39:48 EDT 2004


> >>> a.has_key('oranges')
> 
> I get:
> 
> Traceback (most recent call last):
>   File "<pyshell#30>", line 1, in -toplevel-
>     a.has_key['oranges']
> AttributeError: itemList instance has no attribute 'has_key''
> 
> I had thought 'has_key()' was a builtin, 

Its a part of builtin dictionaries but you aren't using 
a builtin dictionary you are using your own class:

> class itemList:
>     def __init__(self):
>     def addItem(self, item):
>     def addPart(self, item, key, value):
>     def view(self, item=None):
>     def remove(self, item):
>     def count(self):
> 
> a=itemList()

Which as you can see, has no has_key() operation...

YOu either need to subclass your class from dictionary or 
provide a has_key() method.

Alan G.



More information about the Tutor mailing list