Is it an pythonic way to re-assign variables with values of different types?
I have a string of the form k1:v1,k2:v2,k3:v3, etc which comes from the command line. It is straightforward to parse it:
kvs=args.kvs
if kvs is not None:
kvs = [(kv[0], kv[1]) for kv in [kv_str.split(':') for kv_str in kvs.split(',')]]
Since I came to python from strictly-typed languages the approach does not look really clean to me since it is re-assigned with a different type.
Would it be considered a pythonic solution of parsing the string?
kvs = [tuple(kv.split(':')) for kv in kvs.split(',')]vvalues can contain a:, right? If they can, usesplit(':', 1).argparse, you might consider using a customtypeto unpack the string during parsing, rather than after.p.add_argument('--kvs', type=lambda kvs: [tuple(kv.split(':', 1)) for kv in kvs.split(",")]).