35

Are there any downsides to using an empty tuple as a default for an iterable argument to a function? Assuming that what you want in the function is an immutable iterable. e.g.

def foo(a, b=()):
  print a
  for x in b:
    print x

I can't seem to find many examples of this use case.

8
  • Does the function do anything outside of the for loop? Commented Oct 29, 2013 at 17:26
  • 1
    Am I missing something? The only way that the default argument would be used is if you manually call foo without arguments, as foo(). But if you know the function only does anything if the iterable is nonempty, why would you ever write that? Commented Oct 29, 2013 at 17:28
  • 3
    Note that all invocations of the function shares the object in default argument. So a mutable object in default argument may get modified and cause error. I guess using a immutable tuple here is harmless. Commented Oct 29, 2013 at 17:31
  • 1
    .. and now you've edited the function so it does do something outside the for loop, which means your answer "No" to my question should really have been "Yes, my function does do things other than the for loop".. Commented Oct 29, 2013 at 17:32
  • 1
    I misunderstood your first question, sorry about that. Either way, I think the core of the question is whether or not empty tuple's work as default arguments without any unintended side effects. Commented Oct 29, 2013 at 17:36

2 Answers 2

26

I can't think of any downsides, for when you need an immutable iterable. I think it just isn't used because the default_list=None and default_list = default_list or None pattern is what is used for mutable iterables, and people don't bother to change it (as there is no real need) in the less frequently occuring cases when the iterable is immutable. There is certainly no unexpected behaviour as with mutable default arguments.

Sign up to request clarification or add additional context in comments.

Comments

7

There are no intrinsic problems with designing your default to be an immutable empty tuple. However, it could be considered an non-intuitive design approach. Any references to specific locations in the tuple would cause an exception to be raised. If you are careful enough with creating argument checks then this will not be a problem, but if you build the rest of your code to expect data from specific locations and don't verify the tuple is empty then this will cause errors.

It depends on what your goal is for the argument. An empty list would have more obvious applications for a default argument (at least those that modify the list in some way), but an empty immutable tuple would not have any intuitive uses as a default besides indicating that no argument was given.

The typical approach for default arguments would be to set them to None which makes it perfectly clear when the arguments have not been set:

def foo(a, b=None):

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.