When is it best to use a class in Python?

后端 未结 4 2041
小鲜肉
小鲜肉 2021-01-02 10:47

I\'m new to python and programming in general, so would really appreciate any clarification on this point.

For example, in the following code:

    #         


        
4条回答
  •  猫巷女王i
    2021-01-02 11:27

    Your example is rather simplified.

    In a more complete example fighting wouldn't just display the current state - it would also modify that state. Your monster might get hurt and that would change its hit points and morale. This state has to be stored somewhere. If you use a class it would be natural to add instance variables to store this state.

    Using only functions it would be more difficult to find a good place to store the state.

提交回复
热议问题