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

Emile van Sebille emile at fenx.com
Thu Apr 9 20:40:04 CEST 2009


Alexander Daychilde (Gmail) wrote:
<snip>
> class TestObject:
>     def __init__(self):
>         # options and arguments from commandline-->OptionParser
>         self.opt = ""
here, self.opt is initialized as a string...
>         self.args = []
>     def load_cfg(self):
>         # read+parse commandlnie
>         self._parse_commandline()
>     def load_ini(self):
>         ##### I'm trying to get at 'inifile' from the commandline... #####
>         print self.opt['inifile']
... and here you're accessing it as a dict
>     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()
... and here it's changed to whatever is returned by parser.parse_args
> 
> if __name__ == '__main__':
>     # parses command-line argumenmts
>     from optparse import OptionParser
>     test = TestObject()
... so when __init__ has complete, test.opt will be a string
>     test.load_cfg()
... and after this it's whatever is returned by parser.parse_args

try print test.opt to see what was returned -- that'll probably help 
clear things up.  If it's really a list type, you don't access those 
with keys, so self.opt['inifile'] would fail.

>     test.load_ini()


HTH,

Emile



More information about the Tutor mailing list