31 | def listify_args(*args, **kwargs): |
32 | """ A simple way to accept arguments that can be an Iterable or a bunch of arguments that should be treated as a single Iterable. |
33 | (This method is pretty trivial, but I find myself doing it a whole lot.) The reason you can't just use `list()` |
34 | for this purpose is that if an Iterable is given as the only argument, `list(args)` will return a list of Iterables |
35 | rather than a single Iterable. |
36 | |
37 | :Examples: |
38 | |
39 | >>> def test(*args): |
40 | ... return listify_args(*args) |
41 | ... |
42 | >>> test(1,2,3) |
43 | [1, 2, 3] |
44 | >>> test([1,2,3]) |
45 | [1, 2, 3] |
46 | >>> test([1,2,3], 4) |
47 | [[1, 2, 3], 4] |
48 | >>> test("1,2,3", 4) |
49 | ['1,2,3', 4] |
50 | >>> test("1,2,3") |
51 | ['1,2,3'] |
52 | |
53 | """ |
54 | ignore = kwargs.pop('ignore', basestring) |
55 | if len(args) == 1 and isinstance(args[0], Iterable) and not isinstance(args[0], ignore): |
56 | return list(args[0]) |
57 | else: |
58 | return list(args) |