When should I be using classes in Python?

后端 未结 5 1981
星月不相逢
星月不相逢 2020-11-28 17:21

I have been programming in python for about two years; mostly data stuff (pandas, mpl, numpy), but also automation scripts and small web apps. I\'m trying to become a bette

5条回答
  •  我在风中等你
    2020-11-28 17:47

    Whenever you need to maintain a state of your functions and it cannot be accomplished with generators (functions which yield rather than return). Generators maintain their own state.

    If you want to override any of the standard operators, you need a class.

    Whenever you have a use for a Visitor pattern, you'll need classes. Every other design pattern can be accomplished more effectively and cleanly with generators, context managers (which are also better implemented as generators than as classes) and POD types (dictionaries, lists and tuples, etc.).

    If you want to write "pythonic" code, you should prefer context managers and generators over classes. It will be cleaner.

    If you want to extend functionality, you will almost always be able to accomplish it with containment rather than inheritance.

    As every rule, this has an exception. If you want to encapsulate functionality quickly (ie, write test code rather than library-level reusable code), you can encapsulate the state in a class. It will be simple and won't need to be reusable.

    If you need a C++ style destructor (RIIA), you definitely do NOT want to use classes. You want context managers.

提交回复
热议问题