Python 如何正确创建实用程序类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19620498/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 14:12:27  来源:igfitidea点击:

How to create a utility class correctly

pythonclass

提问by Weiner Nir

I have a file that is meant to be a utility file. The file should contain a lot of static methods.

我有一个文件,它是一个实用程序文件。该文件应该包含很多静态方法。

Should I define the methods inside a class this way:

我应该以这种方式在类中定义方法吗:

#utility.py
class utility(object):
    @staticmethod
    def method1(a,b,c):
        pass

    @staticmethod
    def method2(a,b,c):
        pass

or use it like this (without a class):

或者像这样使用它(没有类):

#utility.py
def method1(a,b,c):
    pass

def method2(a,b,c):
    pass

采纳答案by Games Brainiac

The second option is the modus operandi in Python. I mean, if all you're doing is importing functions, then you can do something like this:

第二个选项是 Python 中的作案手法。我的意思是,如果您所做的只是导入函数,那么您可以执行以下操作:

from utility import some_func

which will import your function.

这将导入您的功能。

Best practice is if you're using only static functions, then just put them in the global namespace of a separate module, it will make your life a lot easier. What you're trying to do is make objects and just fill them in with static methods. Why do this, when you can just define the functions in a .pyfile?

最佳实践是,如果您只使用静态函数,那么只需将它们放在一个单独模块的全局命名空间中,这会让您的生活轻松很多。您要做的是创建对象并用静态方法填充它们。当您可以在.py文件中定义函数时,为什么要这样做?

In fact, what you're trying to do hasbeen done. You're trying to store away some good utility functions. Well, python-requests, is a third party library that is just adored by the majority of Pythonistas just does this. It stores away its good utility functions in a separate module. Here is the example.

事实上,你正在做什么已经被完成。您正在尝试存储一些好的实用功能。嗯,python-requests, 是大多数 Pythonistas 喜欢的第三方库,它就是这样做的。它将其良好的实用功能存储在一个单独的模块中。这是示例。

回答by Games Brainiac

While this question is a little opinion based, I'd say the second one is better. It reduces redundancy. Using the first method, you will have to do:

虽然这个问题有点基于意见,但我会说第二个更好。它减少了冗余。使用第一种方法,您必须执行以下操作:

import utility
utility.utility.method1(...)

or:

或者:

from utility import utility
utility.method1(...)

Using the second one however allows you to simply do:

但是,使用第二个允许您简单地执行以下操作:

import utility
utility.method1(...)

or:

或者:

from utility import method1
method1(...)

If you are making a class that only contains static methods, my question is "why do you need the class?" It contributes nothing positive here.

如果你正在创建一个只包含静态方法的类,我的问题是“你为什么需要这个类?” 它在这里没有任何积极意义。

回答by Crowman

Classes encapsulate both data, and behavior, so as general rules:

类封装了数据和行为,因此作为一般规则:

  • If you have something only with data, and no methods, it should probably be a namedtuple, not a class, unless you need to modify that data after creating it.
  • If a function accesses instance data, it should be a method.
  • If a function accesses no instance data, but does access class data, it should be a @classmethod.
  • If a function accesses neither instance data nor class data, it should be a standalone function, unless there's some really compelling reason to make it a @staticmethod.
  • If a classonly has one method, or one method in addition to __init__(), then you almost certainly can and should rewrite it as a function.
  • 如果您只有数据而没有方法,则它可能应该是 anamedtuple而不是 a class,除非您需要在创建数据后修改该数据。
  • 如果一个函数访问实例数据,它应该是一个方法。
  • 如果一个函数不访问实例数据,但访问类数据,它应该是一个@classmethod.
  • 如果一个函数既不访问实例数据也不访问类数据,则它应该是一个独立的函数,除非有一些真正令人信服的理由将其设为@staticmethod.
  • 如果 aclass只有一种方法,或者除了 之外还有一种方法__init__(),那么您几乎肯定可以并且应该将其重写为函数。

Classes are really easy to abuse, but the temptation to shove everything into a class should really be avoided. You should use them when they make sense, and avoid using them when they don't.

类真的很容易被滥用,但是真的应该避免将所有东西都塞进一个类中的诱惑。你应该在它们有意义的时候使用它们,并避免在它们没有意义的时候使用它们。