How to define properties in __init__

前端 未结 3 1071

I whish to define properties in a class from a member function. Below is some test code showing how I would like this to work. However I don\'t get the expected behaviour.

3条回答
  •  清歌不尽
    2021-01-05 20:58

    Why are you defining properties at __init__ time? It's confusing and clever, so you better have a really good reason. The loop problem that Stef pointed out is just one example of why this should be avoided.

    If you need to redifine which properties a subclass has, you can just do del self. in the subclass __init__ method, or define new properties in the subclass.

    Also, some style nitpicks:

    • Indent to 4 spaces, not 2
    • Don't mix quote types unnecessarily
    • Use underscores instead of camel case for method names. PropNames -> prop_names
    • PropNames doesn't really need to be a method

提交回复
热议问题