Creating objects based on user input

后端 未结 3 735
轮回少年
轮回少年 2021-01-27 03:37

I have a couple of questions. The code below doesn\'t run because I\'ve specified three arguments to the __init__ method and the make_dog function retu

3条回答
  •  心在旅途
    2021-01-27 03:52

    Yes, you can do this, but you have to use the ** operator to convert the dict to an argument list.

    d = Dog(**make_dog())
    

    Please see What does ** (double star) and * (star) do for Python parameters? for further info.

    Also see Unpacking Argument Lists in the official Python tutorial.

提交回复
热议问题