wasp wasp - 3 months ago 21
Python Question

Is it Pythonic to passed in arguments in a function that will be used by a function inside it?

Is there a better way to do this? Like I'm passing in arguments to

that will be used in

def inside_func(arg1,arg2):
print arg1, arg2

def func(arg1, arg2):


Of course it is.

Your outer function provides a service, and to do its job it may need inputs to work with. How it uses those inputs is up to the function. If it needs another function to do their job and they pass in the arguments verbatim, is an implementation detail.

You are doing nothing more than standard encapsulation and modularisation here. This would be correct programming practice in any language, not just Python.

The Python standard library is full of examples; it is often used to provide a simpler interface for quick use-cases. The textwrap.wrap() function for example:

def wrap(text, width=70, **kwargs):
    """Wrap a single paragraph of text, returning a list of wrapped lines.

    Reformat the single paragraph in 'text' so it fits in lines of no
    more than 'width' columns, and return a list of wrapped lines.  By
    default, tabs in 'text' are expanded with string.expandtabs(), and
    all other whitespace characters (including newline) are converted to
    space.  See TextWrapper class for available keyword args to customize
    wrapping behaviour.
    w = TextWrapper(width=width, **kwargs)
    return w.wrap(text)

This does nothing else but pass the arguments on to other callables, just so your code doesn't have to remember how to use the TextWrapper() class for a quick one-off text wrapping job.