When is it best to use a class in Python?

后端 未结 4 2002
小鲜肉
小鲜肉 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条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-02 11:12

    Your example isn't particularly interesting — it just prints three numbers. You don't need classes, or even a function (really) to do that.

    But if you ever need to write a program that has to keep track of two separate monsters at one time, know their health, differentiate their fighting abilities, and so, you can see the value of encapsulating each of the monsters in a separate monster instance.

提交回复
热议问题