I'm faint why this can't work

Gary Herron gherron at digipen.edu
Fri Feb 16 03:00:58 EST 2007


JStoneGT at aol.com wrote:
> Hello,
>  
> I got this similar sample script from books:
>  
> $ cat sampdict.py
> #!/usr/bin/python
> class SampDict(dict):
>     def __init__(self, filename=None):
>         self["name"] = filename
>  
> But when I run it I got the errors:
>  
> >>> from sampdict import SampDict
> >>> SampDict("/etc/passwd")     
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
>   File "sampdict.py", line 4, in __init__
>     self["name"] = filename
> AttributeError: SampDict instance has no attribute '__setitem__'
>  
>  
> I'm using Python 2.3.4.
>  
> Please help.Thanks.
It works just fine in all versions of Python I could find: 2.3.3, 2.3.5, 
2.4, and 2.5.

My only guesses:

Have you mixed spaces and tabs for the indentation, so that the code 
Python sees is not as we see it printed?  (Don't ever do that.)

Do you have several copies of sampdict.py (or even sampdict.pyc) so that 
the copy being imported is not the one you printed?  (Run Python with a 
-v switch to test for this possibility.)

Gary Herron






More information about the Python-list mailing list