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

Alexander Daychilde (Gmail) daychilde at gmail.com
Thu Apr 9 17:47:59 CEST 2009

I feel like I'm missing something simple, but I have now spent hours
googling for an answer. I think I must not be searching for the right terms,
or else this is something I'm not supposed to be doing - but it seems
straightforward to me.


Here's my test code (condensed from the actual much longer code I'm adapting
from functions to objects - I'm new to OOP, but I'm trying J ):




class TestObject:


    def __init__(self):


        # options and arguments from commandline-->OptionParser

        self.opt = ""

        self.args = []


    def load_cfg(self):


        # read+parse commandlnie



    def load_ini(self):


        ##### I'm trying to get at 'inifile' from the commandline... #####

        print self.opt['inifile']


    def _parse_commandline(self):


        parser = OptionParser()


        parser.add_option("-i", dest = "inifile", help = "specify an ini
file to load")


        (self.opt, self.args) = parser.parse_args()


if __name__ == '__main__':


    # parses command-line argumenmts

    from optparse import OptionParser


    test = TestObject()








In the middle is a comment with five hashes to show you the crux of my


If I eliminate "['inifile']", I can print the list.


How in the world can I get just the 'inifile' element?


Ideally, of course, I'm not printing this - I'm going to access it from
outside the class, or even inside the class.


The error I get when running the above code:


Traceback (most recent call last):

  File "listinclass.py", line 34, in <module>


  File "listinclass.py", line 17, in load_ini

    print self.opt['inifile']

AttributeError: Values instance has no attribute '__getitem__'


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.


Many thanks for your consideration.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090409/5a2cc2e3/attachment.htm>

More information about the Tutor mailing list