什么是分类的一般方法? [英] what's the general way of separating classes?

查看:90
本文介绍了什么是分类的一般方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据我对C#的简短经验,我了解到将每个类放在一个单独的文件中是非常标准的。我认为这是一个编译语言的好处,然后可以将文件组合在一起。


我想知道这是怎么回事用Python处理?将类放入单独的模块中是否正常?b $ b是正常的?看起来这可能会失控,因为模块是彼此分开的而不是编译在一起的
。你最终会得到很多重要的声明。


那么相关的课程会被放入一个模块吗?或者是否有一些

推荐的方法来处理这个问题?


谢谢。

解决方案

Em Seg,2006-03-20?* s 15:42 +0000,John Salerno escreveu:

相关的类是否被放入单个模块中?或者是否有一些
推荐的方法来处理这个?




恕我直言,如果它们可以放在同一个文件上而不会创建一个巨大的文件/>
(3000+行),然后去做。如果你不能,那么而不是创造一个巨大的



program / module.py


你可以做很多


program / modules / * .py


并创建一个程序/ modules / __ init__.py有一些东西喜欢


from program.modules.class1 import class1

from program.modules.class2 import class2


And ,在你的程序的其余部分,你可以通过

导入class1和class2,只需从program.modules import class1,class2
发出


br />
HTH,


-

Felipe。


< blockquote> Felipe Almeida Lessa写道:

并从program.modules.class1 import class1 <创建一个类似于

的程序/ modules / __ init__.py br />来自program.modules.class2 import class2


我不确定我理解这两行的重点,如果你要去

到那时这样做:

并且,在你的程序的其余部分,你可以通过
从program.modules导入class1,class2 <
导入class1和class2 / blockquote>


如果您仍然在程序的每个其他模块中单独导入

class1和class2,__ init__文件如何提供帮助?


John Salerno< jo ****** @ NOSPAMgmail.com>写道:

根据我对C#的简短经验,我了解到将每个类放在一个单独的文件中是非常标准的实践。我认为这是编译语言的一个好处,然后可以将文件组合在一起。

我想知道这是如何在Python中正常处理的?将类放入单独的模块中是否正常?看起来这可能会失控,因为模块彼此分离而不是编译在一起。你最终会得到很多导入语句。

然后将相关的类放入单个模块中吗?或者是否有一些
推荐的方法来处理这个问题?




这里有两个不同的问题。一个是如何组织你的
源代码,另一个是你向用户公开的API。


从源代码的角度来看,我会通常在每个文件中放一个

类,但也有例外(没有双关语)。如果

我有一堆非常密切相关的课程,特别是如果它们很小,我会把多个课程放在一个文件中。

小到多小?没有硬数字,但任何超过

两种方法的东西可能都不小。肯定是例外课程

没有得到自己的档案;他们被放入他们制作的模块中

最有意义。


从API的角度来看,仅仅因为你将课程分开了
进入多个模块,并不意味着用户必须看到

的复杂性。有一个顶级模块导入其他模块。

用户只需要导入顶级用户。


这里没有神奇的答案。


为了给你一个真实的例子,我刚刚看了一下我b / b
最近做的一个项目。我有8个python源文件,总计1505行。我定义了

14个类,其中7个是异常类。我的顶级模块

(用户将导入的模块)没有定义类;它导入

其他模块并具有一堆工厂功能。我看了

一个包含两个真实类的文件;其中一个是

本质上是一个内部类,只有两个方法; __init __()和

__call __(),两者都很简单。


From my brief experience with C#, I learned that it was pretty standard
practice to put each class in a separate file. I assume this is a
benefit of a compiled language that the files can then be grouped together.

What I''m wondering is how is this normally handled in Python? Is it
normal for classes to be put in separate modules? It seems like this can
get out of hand, since modules are separate from one another and not
compiled together. You''d end up with a lot of import statements.

Are related classes put into a single module then? Or is there some
recommended method for how to handle this?

Thanks.

解决方案

Em Seg, 2006-03-20 ?*s 15:42 +0000, John Salerno escreveu:

Are related classes put into a single module then? Or is there some
recommended method for how to handle this?



IMHO, if they can be put on the same file without creating a huge one
(3000+ lines), then do it. If you can''t, then instead of creating a
huge:

program/module.py

You can do lots of

program/modules/*.py

And create an program/modules/__init__.py that have something like

from program.modules.class1 import class1
from program.modules.class2 import class2

And, in the rest of your program, you can import class1 and class2 by
just issuing

from program.modules import class1, class2

HTH,

--
Felipe.


Felipe Almeida Lessa wrote:

And create an program/modules/__init__.py that have something like

from program.modules.class1 import class1
from program.modules.class2 import class2
I''m not sure I understand the point of those two lines, if you are going
to then do this:
And, in the rest of your program, you can import class1 and class2 by
just issuing

from program.modules import class1, class2



How does the __init__ file help if you are still individually importing
class1 and class2 in each other module of your program?


John Salerno <jo******@NOSPAMgmail.com> wrote:

From my brief experience with C#, I learned that it was pretty standard
practice to put each class in a separate file. I assume this is a
benefit of a compiled language that the files can then be grouped together.

What I''m wondering is how is this normally handled in Python? Is it
normal for classes to be put in separate modules? It seems like this can
get out of hand, since modules are separate from one another and not
compiled together. You''d end up with a lot of import statements.

Are related classes put into a single module then? Or is there some
recommended method for how to handle this?



There are two different issues here. One is how you organize your
source code, the other is what API you expose to the user.

From the source code point of view, I will generally put a single
class in each file, but there are exceptions (no pun intended). If
I''ve got a bunch of very closely related classes, especially if
they''re small, I''ll put more than one class in a file. How small is
small? There''s no hard number, but anything that''s got more than a
couple of methods is probably not small. Exception classes certainly
don''t get their own file; they get put in the module where they make
the most sense.

From the API point of view, just because you split your classes up
into multiple modules, doesn''t mean the user has to see that
complexity. Have one top level module which imports the others. The
user only has to import the top level one.

There''s no magic answer here.

To give you a real example, I just took a look at a recent project I
did. I''ve got 8 python source files, totalling 1505 lines. I define
14 classes, of which 7 are exception classes. My top level module
(the one a user would import) has no classes defined in it; it imports
the other modules and has a bunch of factory functions. I looked at
the one file that contains two real classes; one of them is
essentially an inner class and has only two methods; __init__() and
__call__(), both of which are trivial.


这篇关于什么是分类的一般方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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