Python - 我应该把我的帮助函数放在课外吗? [英] Python - Should I put my helper functions inside or outside the class?

查看:199
本文介绍了Python - 我应该把我的帮助函数放在课外吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Python中,如果一个类的某些方法需要一个帮助函数,但是帮助函数本身并没有使用任何类,那么我应该将帮助函数放在类之外吗?

In Python, if some methods of a class need a helper function, but the helper function itself doesn't use anything in the class, should I put the helper function inside or outside the class?

我试着把它放在里面,但是PyLint抱怨这个功能可以放在外面...

I tried putting it inside but PyLint was complaining that this function could have been put outside...

另外,有没有好书谈论这种东西(它不一定是Python)?

Also, are there any good books talking about this kind of stuff (it doesn't have to be Python)?

感谢您的帮助!

杰克

@Karl:

该类是一个软件升级程序,如果文件夹不存在,帮助函数将创建一个新文件夹。该课程在一个模块中几乎只有现在的类的代码。

The class is a software upgrader and the helper function creates a new folder if the folder doesn't exist yet. The class is in a module having pretty much only the code for the class as of now. Other classes may be added later on.

感谢
杰克

推荐答案

在决定放置助手功能的地方,我问的问题是,只适用于这个类吗?如果可以在其他地方有所帮助,那么它将在模块级别进行;如果它确实只适用于这个类,那么它会在类中使用 staticmethod (不需要类数据来完成它的工作)或 classmethod (使用一些类,但不是实例,数据来做它的工作)。

When deciding where to put helper functions the question I ask is, "Is it only for this class?" If it can help in other places, then it goes at the module level; if it is indeed only for this class, then it goes in the class with either staticmethod (needs no class data to do its job) or classmethod (uses some class, but not instance, data to do its job).

另一个python代码检查器是 pyflakes

Another python code checker is pyflakes.

这篇关于Python - 我应该把我的帮助函数放在课外吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆