0

I want to write a function with an argument which can be either:

  • a single argument
  • a list of arguments

E.g. symlink(target_or_targets, destination), which could be called as either:

  • symlink(target, destination)
  • symlink([target1, target2], destination)

How do I test for a single argument, and convert it into a list with one item?

(Note: I don't want to use a decorator or *args)

Tom Hale
  • 40,825
  • 36
  • 187
  • 242

1 Answers1

1

Convert to list if given a single argument

Stricter version:

if type(target_or_targets) not in (list, set, tuple):
    targets = [target_or_targets]

More permissive:

if not isinstance(target_or_targets, (list, set, tuple)):
    targets = [target_or_targets]
Tom Hale
  • 40,825
  • 36
  • 187
  • 242