[Tutor] Accessing a list inside a class...

spir denis.spir at free.fr
Thu Apr 9 18:42:46 CEST 2009


Le Thu, 9 Apr 2009 08:47:59 -0700,
"Alexander Daychilde (Gmail)" <daychilde at gmail.com> s'exprima ainsi:

> The error I get when running the above code:
> 
> Traceback (most recent call last):
> 
>   File "listinclass.py", line 34, in <module>
> 
>     test.load_ini()
> 
>   File "listinclass.py", line 17, in load_ini
> 
>     print self.opt['inifile']
> 
> AttributeError: Values instance has no attribute '__getitem__'

This means the following:
* You're trying to access an item inside something called self.opt by using a key: 'inifile'.
* self.opt should then be able to return you an item from a key, in other words it should be an object that behaves like a dictionary, right?
* The "magic" method the allows this behaviour is called __getitem__ (the same as for list indexing, actually). So that python looks for this method in the class of self.opt, that happens to be Values, and tells you...

Sure, not very clear!

> I've googled for "list in a class" and pretty much every variant I can think
> of, and I just can't seem to find a single example of someone trying to get
> to an element of a list stored in a class. so I apologize for asking a
> basic/simple question, but I just can't find it.

The issue has nothing to do with the fact the self.opt happens to be an attribute.

Denis
------
la vita e estrany


More information about the Tutor mailing list