Passing parameters at the command line (New Python User)

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Mon Sep 24 05:03:16 EDT 2007


On Mon, 24 Sep 2007 01:04:58 -0700, cjt22 wrote:

>     for i in range(len(args)):
>         if args[i] == "-no":
>             Initialise.init(0)
>             Process.processCon(file2,1)
>             Output.print()
> 
>         if args[i] == "-not":
>            Initialise.init(1)
>             Process1.process(stepStore, firstSteps)
>             Output.print1()

That ``for`` loop is an anti-pattern in Python.  If you want to iterate
over the elements of `args` the just do it directly instead of using an
index:

    for arg in args:
        if arg == '-no':
            # ...

If you need the element *and* an index:

    for i, arg in enumarate(args):
        # ...

Ciao,
	Marc 'BlackJack' Rintsch



More information about the Python-list mailing list