I find myself often having a function, for example authenticate:

authenticate(user, token):
    # do authentication

and a dictionary created by reading a configuration file, like this:


My question is, in terms of best practice (python in specific), should I just call my function by directly giving the arguments, like this:

authenticate(conf['general']['auth_user'], conf['general']['auth_token'])

or should I assign the values into more readable variables and then call the function?

user = conf['general']['auth_user']
token = conf['general']['auth_token']

authenticate(user, token)

Personally, I find the second way more readable, especially when the function needs more than 2 arguments.

  • I would extract the 'general', 'auth_user', and 'auth_token' strings to constants. I would also make a class that initializes itself from the configuration dictionary, and provides these fields as properties
    – Alexander
    Jun 8, 2017 at 17:51

2 Answers 2


This question has little to do with Python or dictionaries. The more general question is when should I introduce variables for subexpressions of an expression? See also this SE question for more discussion. Here are some good reasons I can think of for doing so

  1. Pulling out the sub-expression makes the expression easier to read.
  2. The sub-expression results in a domain-object important enough to warrant a name.
  3. Pulling out the sub-expression allows you to avoid repeated computation.

I think in this case you have a good case that your change fulfills 1 and 2 and so sure, it's a fine change to make.


In Python, the best way I can think of is to use named arguments for this:

 authenticate(user = conf['general']['auth_user'], 
              token = conf['general']['auth_token'])

(of course, you will have to break this down on two lines to keep the code readable). This way, you do not only provide explaining variables, you also make the statement independent from the order in which the parameters are passed to authenticate.

  • I would disagree in this case. user = and token = don't provide any information that 'auth_user' and 'auth_token' didn't already.
    – Alexander
    Jun 8, 2017 at 17:50
  • @Alexander: you missed one important point: using named arguments prevents against accidentally interchanging the order of arguments. Edited my answer accordingly.
    – Doc Brown
    Jun 8, 2017 at 21:00

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.