Search code examples
pythonargumentsmaintainability

Proper use vs. over use of *args in Python


I've been looking at the source code for an open source package that I'm using. Nearly every function uses *args instead of named arguments. I'm finding it hard to follow and use the code, because every time I want to call a function I have to go back, pick through the source code, and identify what the arguments should be, and what order they should be in. The question I have is this: Is there a compelling reason to use *args in every function, or is this an abuse of the concept? Thanks, -b


Solution

  • This might be a personal reasoning but I only use *args and **kwargs when I have a lot of optional fields where some fields are only used in a certain context.

    The only other occasion I used args is when I was building an XML RPC client for a cloud API. Since I was just passing parameters to an underlying layer and since the function where generated dynamically, I had no other choices but to use *args as I had no way to know all the parameters in advance.

    In most case, you won't even need it. I consider it to be mostly laziness than anything else.

    Some people who comes from Java and C# might use this as a replacement for "params", but there are so many way to pass optional parameters in python.

    And I agree that even if you use *args, you should have a very good documentation.