0

I'm trying to learn Python based on Python Crash Course by 2nd edition by Eric Matthes.

I tried this excersice working with kwargs in pycharm. Pycharm greys out my **extra parameter. Why ?

def cars(manufacturer, modelname, **extra):
    auto_info['fabriekant'] = manufacturer
    auto_info['model'] = modelname
    return auto_info

autoprofiel = cars('Volvo', 'xv40', color='black', cruize_control=True)
print(autoprofiel)
  • It looks exactly the same like the sample code to me.
def build_profile(first, last, **user_info):
    'Build a dictionary containing everything we know about a user.'
    user_info['first_name'] = first
    user_info['last_name'] = last
    return user_info

user_profile = build_profile('Albert','einstein',location='princeton',field='physics')
print(user_profile)

buran
  • 13,682
  • 10
  • 36
  • 61
  • 4
    The difference is that you don't use `extra` inside your function. It's just the IDE telling you that it is unused. Also I guess you want to replace `extra` with `auto_info`, otherwise you will get error. – buran Oct 31 '21 at 10:13
  • Thank you Buran, that solved a mistery. So the *kwargs has to be the name of the dictionary you return and use. – TheOriginalStinger Oct 31 '21 at 11:18
  • We use `**kwargs` (and `*args`) by convention. As you can see from both the example and your code, any other valid name will do. Check https://stackoverflow.com/questions/36901/what-does-double-star-asterisk-and-star-asterisk-do-for-parameters – buran Oct 31 '21 at 11:29

0 Answers0