Python - 我应该把我的帮助函数内部还是外面的类? [英] Python - Should I put my helper functions inside or outside the class?

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

问题描述

在Python中,如果一个类的某些方法需要一个辅助函数,但辅助函数本身不使用类中的任何东西,我应该将辅助函数放在类内部还是外部?



我试着把它放在里面,但PyLint抱怨这个函数可能被放在外面...



谈论这种东西(它不一定是Python)?



感谢您的帮助!



杰克



@Karl:



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

解决方案



div>

当决定把辅助函数放在哪里时,我问的问题是这只是这个类吗?如果它可以在其他地方帮助,那么它在模块级别;如果它确实只对这个类,那么它进入类与 staticmethod (不需要类数据来完成它的工作)或 classmethod (使用一些类,但不是实例,数据来完成它的工作)。



另一个python代码检查器是pyflakes


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?

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

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

Thanks for your help!

Jack

@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.

Thanks, Jack

解决方案

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).

Another python code checker is pyflakes.

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

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