pythonic equivalent of upvar?

Peter Otten __peter__ at web.de
Tue Dec 20 09:36:03 EST 2005


David MacKay wrote:

> I'm trying to write something with the same brevity
> as perl's one-liner
> 
> eval "\$$1=\$2" while @ARGV && $ARGV[0]=~ ^(\w+)=(.*) && shift;

import optparse

parser = optparse.OptionParser("See the error of your ways and use
optparse")
parser.add_option("--decode", action="store_true")
parser.add_option("--verbose", action="store_true")
parser.add_option("--bits", type="int", default=7)
parser.add_option("-N", type="int", default=10000)
parser.add_option("--file", default="blah")

options, args = parser.parse_args()
if args:
    parser.error("unexpected positional argument(s)")

# accessing option values
print options.bits

Peter 




More information about the Python-list mailing list