[Python-de] Zuweisungen bei if?

Christopher Arndt chris at chrisarndt.de
Di Apr 10 09:45:00 CEST 2012


On 10.04.2012 08:17, Ulli Horlacher wrote:
> Perl:
> 
>    if (@ARGV and $ARGV[0] =~ /(\w+)/) { print "$1\n" }
> 
> Python:

    import optparse

    optparser = optparse.OptionParser()
    optparse.add_option('-f', '--foo', dest='foo',
        action="store_true", help='Make it foo-ish')
    optparse.add_option(...)

    options, args = optparser.parse_args()

    if options.foo:
        ...

    if not args:
        optparser.print_help()

http://docs.python.org/library/optparse.html

Oder, ab Python 2.7, alternativ:

http://docs.python.org/library/argparse.html


Chris


Mehr Informationen über die Mailingliste python-de