my first class: Args

Scott David Daniels Scott.Daniels at Acm.Org
Sun Aug 29 08:31:42 EDT 2004


Peter Kleiweg wrote:
> Scott David Daniels schreef:
>> > class Args: ...
>> >     def __init__(self,usage=<some text>):
>> > ...
>> >         self._argv = sys.argv[1:]
 >>if __name__ == '__main__':
 >>      <lotsa code>
-->
>>   class Args: ...
>>       def __init__(self, progname, args, usage=<some text>):
>>
>>   def demo(progname, args):
>>       a = Args(progname, args,
>>         'Usage: %(progname)s [-a value] [-b value] [-c] word [file...]')
>>       <lotsa code 'much like' above>
>>
>>if __name__ == '__main__':
>>      demo(sys.argv[0], sys.argv[1:])
 >
 > This goes against the purpose of the class: to take care of as
 > much of the overhead of script writing as possible. So I can
 > simply do this:

^^ By the way, this goes at the top of your module comment -- it
   lets users understand in 3 (or 6 if you add the example) lines.

 >
 >     import args
 >
 >     a = args.Args()
 >     for line in a: ...

I am not saying you are wrong, exactly, but I will explain why I
prefer my style.  In my style, I increase the chance that I can
reuse programs without changing (or even really reading) them.
This allows something else to get "in the way" when you decide
to build another application from some you already have.

For example:

     import sys, app1, app2

     progname = sys.argv[0]
     break = sys.argv.index(1, ';')
     app1.main(progname + 'part1', sys.argv[1 : sep])
     app2.main(progname + 'part2', sys.argv[sep :])


Perhaps a compromise between the two styles would be closer to
your aesthetic, where the call is:

>     a = args.Args(sys.argv)
>     for line in a: ...

More work on combining, but less work on using.  At any rate it has
been fun blathering about style.


--Scott David Daniels
Scott.Daniels at Acm.Org



More information about the Python-list mailing list