我已经成为Python科学程序员几年了,随着我的程序越来越大,我发现自己遇到了一个特定的问题.我是自学成才的,所以我从未接受过任何正式的培训,并且真正地花了一些时间适当"地使用Python进行常规"编码.
I have been a Python Scientific Programmer for a few years now, and I find myself coming to a sort specific problem as my programs get larger and larger. I am self taught so I have never had any formal training and spent any time really on 'conventions' of coding in Python "properly".
无论如何,到目前为止,我发现自己总是创建一个utils.py文件,该文件存储了程序使用的所有已定义函数.然后,我发现自己将这些功能归为各自的目的.我知道对事物进行分组的一种方法当然是使用类,但是我不确定我的策略是否与实际应使用的类相抵触.
Anyways, to the point, I find myself always creating a utils.py file that I store all my defined functions in that my programs use. I then find myself grouping these functions into their respective purposes. One way of I know of grouping things is of course using Classes, but I am unsure as to whether my strategy goes against what classes should actually be used for.
假设我有一堆函数,它们的功能大致相同,如下所示:
Say I have a bunch of functions that do roughly the same thing like this:
def add(a,b): return a + b def sub(a,b): return a -b def cap(string): return string.title() def lower(string): return string.lower()现在显然,这4个函数可以看作是做两个单独的目的,一个是计算,另一个是格式化.这就是逻辑告诉我要执行的操作,但是由于我不想初始化与该类永远对应的变量,因此必须解决该问题.
Now obviously these 4 functions can be seen as doing two seperate purposes one is calculation and the other is formatting. This is what logic tells me to do, but I have to work around it since I don't want to initialise a variable that corresponds to the class evertime.
class calc_funcs(object): def __init__(self): pass @staticmethod def add(a,b): return a + b @staticmethod def sub(a, b): return a - b class format_funcs(object): def __init__(self): pass @staticmethod def cap(string): return string.title() @staticmethod def lower(string): return string.lower()通过这种方式,我现在将这些方法分组"到一个不错的程序包中,该程序包可以根据它们在程序中的作用来更快地找到所需的方法.
This way I have now 'grouped' these methods together into a nice package that makes finding desired methods much faster based on their role in the program.
print calc_funcs.add(1,2) print format_funcs.lower("Hello Bob")不管怎么说,我觉得这是一种非常"unpython-y"的处理方式,而且感觉很混乱.我要考虑正确的方法还是有其他方法?
However that being said, I feel this is a very 'unpython-y' way to do things, and it just feels messy. Am I going about thinking this the right way or is there an alternate method?
推荐答案另一种方法是制作一个 util package 并将您的函数拆分为该包中的不同模块.软件包的基础知识:创建一个目录(其名称将为软件包名称)并在其中放置一个特殊文件,即 __ init __.py 文件.该可以包含代码,但是对于基本的软件包组织而言,它可以是一个空文件.
Another approach is to make a util package and split up your functions into different modules within that package. The basics of packages: make a directory (whose name will be the package name) and put a special file in it, the __init__.py file. This can contain code, but for the basic package organization, it can be an empty file.
my_package/ __init__.py module1.py/ modle2.py/ ... module3.py假设您位于工作目录中
mkdir util touch util/__init__.py然后在您的 util 目录中,创建 calc_funcs.py
Then inside your util directory, make calc_funcs.py
def add(a,b): return a + b def sub(a,b): return a -b和 format_funcs.py :
def cap(string): return string.title() def lower(string): return string.lower()现在,从您的工作目录中,您可以执行以下操作:
And now, from your working directory, you can do things like the following:
>>> from util import calc_funcs >>> calc_funcs.add(1,3) 4 >>> from util.format_funcs import cap >>> cap("the quick brown fox jumped over the lazy dog") 'The Quick Brown Fox Jumped Over The Lazy Dog'已修改以添加
但是,请注意,如果我们重新启动解释器会话,则:
Edited to add
Notice, though, if we restart the interpreter session:
>>> import util >>> util.format_funcs.cap("i should've been a book") Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: module 'util' has no attribute 'format_funcs'这就是 __ init __.py 的目的!
在 __ init __.py 中,添加以下内容:
import util.calc_funcs, util.format_funcs现在,再次重新启动解释器:
Now, restart the interpreter again:
>>> import util >>> util.calc_funcs.add('1','2') '12' >>> util.format_funcs.lower("I DON'T KNOW WHAT I'M YELLING ABOUT") "i don't know what i'm yelling about"是的!我们可以通过易于导入的方式灵活控制名称空间!基本上, __ init __.py 与类定义中的 __ init __ 方法类似.
Yay! We have flexible control over our namespaces with easy importing! Basically, the __init__.py plays an analogous role to the __init__ method in a class definition.
更多推荐
使用Python中的类对函数进行分组
发布评论