Is self.__dict__.update(**kwargs) good or poor style?

前端 未结 4 877
执笔经年
执笔经年 2020-12-01 04:24

In Python, say I have some class, Circle, that inherits from Shape. Shape needs x- and y-coordinates, and, in addition, Circle needs a radius. I want to be able to initializ

4条回答
  •  情歌与酒
    2020-12-01 04:53

    I would say that the first method is definitely preferable, because explicit is better than implicit.

    Consider what would happen if you made a typo when initializing a Circle, something like Circle(x=1., y=5., rr=3.). You want to see this error immediately, which would not happen with __dict__.update(kwargs).

提交回复
热议问题