0

Given the following two methods:

def test():
   string = "{test}"
   print convert(string, test='test')

def convert(string, **test):
    return string.format(test)

Why does this throw an KeyError: 'test'?

As I have seen in other threads, this should be a valid way of passing values, shouldn't it?

Community
  • 1
  • 1
Frame91
  • 3,670
  • 8
  • 45
  • 89
  • For me it throws `TypeError: format expects arg 2 to be string or unicode, not dict`. – BrenBarn Aug 26 '15 at 04:33
  • Sorry, there was a mistake with format in my version... I changed it in the question accordingly. There should be a key error now – Frame91 Aug 26 '15 at 04:35

1 Answers1

1

As shown in the question you linked to, you need to expand the keyword-argument dictionary when passing it to format:

return string.format(**test)
BrenBarn
  • 242,874
  • 37
  • 412
  • 384
  • Thank you. I didn't notice the ** as parameter for the method. It works now! I'll accept this answer as soon as it is possible – Frame91 Aug 26 '15 at 04:38