Making command-line args available to deeply-nested functions

Loris Bennett loris.bennett at fu-berlin.de
Fri Aug 20 05:53:41 EDT 2021


Hi,

TL;DR:

If I have a command-line argument for a program, what is the best way
of making this available to a deeply-nested[1] function call without
passing the parameter through every intermediate function?

Long version:

If I have, say, a command-line program to send an email with a
personalised salutation, a naive approach to the function calls might
look like the following

  create_email(..., args.salutation_server_credentials)
    create_body(..., args.salutation_server_credentials)
      create_salutation(..., args.salutation_server_credentials)
 
where args.salutation_server_credentials could be given on the
command-line or read from a configuration file by the top-level, but is
only ever actually needed by the create_salutation function.  

I can see that the top-level could just create an object from a class
which encapsulates everything, but what if I want to keep the salutation
generation separate, so that I can have a separate program which just
generates the salutation and print it to the terminal?

I guess I am really asking how to avoid "passing through" arguments to
functions which only need them to call other functions, so maybe the
answer is just to avoid nesting.

Cheers,

Loris
 

Footnotes:

[1] Is a TL;DR allowed to have footnotes?  Probably not, but just to
clarify, I would consider the third of three levels as being
already "deeply-nested".

-- 
This signature is currently under construction.


More information about the Python-list mailing list