为什么每个文件只有一个类?

为什么每个文件只有一个类?

问题描述:

我的一位朋友,拥有Java和Perl的编程背景,每个类在其自己的单独文件中放置
。我告诉他,将所有相关课程保存在一个文件中比使用每个课程一个文件更多地在Python

习语中。他问为什么,坦率地说,他有效的问题让我感到茫然。


我试图通过声称一个文件来合理化这个Python习语 - 一个

模块 - 使得一个很好的代码容器在逻辑上与
结合在一起,例如一个类及其子类。他认为

目录(包)也可以将文件绑定在一起,并且通过将这些类作为单独的文件绑定,他可以差异。他们看看

他们有什么不同,他不可能用代码全部用

一个文件。


我还提出在一个文件下有相关的课程可以直接访问这些课程,例如:


------- ---------------------------

file:foo.py

== =

类酒吧(对象):

通过

===


文件demo1。 $

===

导入foo


barinstance = foo.Bar()

= ==

----------------------------------


vs.


------------------------------- ---

文件:foopkg / bar.py

===

class Bar(对象):

通过

===


档案:demo2.py

===

导入foopkg.bar


barinstance = foopkg.bar.Bar()

===

------- ---------------------------


他没找到我的ar令人信服的说法,所以我想我会在这里要求

看看为什么Python成语是这样的:为什么我们不应该为你自己安排课程?
单独的文件?


非常感谢的想法,评论和见解,

克里斯

A friend of mine with a programming background in Java and Perl places
each class in its own separate file in . I informed him that keeping
all related classes together in a single file is more in the Python
idiom than one file per class. He asked why, and frankly, his valid
question has me flummoxed.

I tried to rationalize this Python idiom by claiming a file--a single
module--makes for a great container of code which is logically tied
together, such as a class and its subclasses. He posited that
directories (packages) can tie the files together just as well, and by
having the classes as separate files, he can "diff" them to see how
they differ, something he wouldn''t be able to do with the code all in
one file.

I also offered that having related classes under one file gives more
direct access to those classes, e.g.:

----------------------------------
file: foo.py
===
class Bar(object):
pass
===

file demo1.py
===
import foo

barinstance = foo.Bar()
===
----------------------------------

versus

----------------------------------
file: foopkg/bar.py
===
class Bar(object):
pass
===

file: demo2.py
===
import foopkg.bar

barinstance = foopkg.bar.Bar()
===
----------------------------------

He doesn''t find my arguments convincing, so I thought I''d ask here to
see why the Python idiom is the way it is: why should we NOT be
placing classes in their own separate files?

Thoughts, comments, and insight much appreciated,
Chris

Chris Lasher napisa3(a):
Chris Lasher napisa3(a):

我的一位朋友,拥有Java和Perl的编程背景

每个类在其自己的单独文件中。我告诉他,将所有相关课程保存在一个文件中比使用每个课程一个文件更多地在Python

习语中。他问为什么,坦率地说,他有效的问题让我感到茫然。


我试图通过声称一个文件来合理化这个Python习语 - 一个

模块 - 使得一个很好的代码容器在逻辑上与
结合在一起,例如一个类及其子类。他认为

目录(包)也可以将文件绑定在一起,并且通过将这些类作为单独的文件绑定,他可以差异。他们看看他们有什么不同,他们不能用代码全部在

一个文件中。
A friend of mine with a programming background in Java and Perl places
each class in its own separate file in . I informed him that keeping
all related classes together in a single file is more in the Python
idiom than one file per class. He asked why, and frankly, his valid
question has me flummoxed.

I tried to rationalize this Python idiom by claiming a file--a single
module--makes for a great container of code which is logically tied
together, such as a class and its subclasses. He posited that
directories (packages) can tie the files together just as well, and by
having the classes as separate files, he can "diff" them to see how
they differ, something he wouldn''t be able to do with the code all in
one file.



我不会试图合理化它,这很自然。

的要求每个模块只需要一个公共类需要合理化(除了编译器的b $ b限制),而不是在
中拥有尽可能多的类的自由
模块是程序'architecure所需要的。


-

Jarek Zgoda
http://jpa.berlios.de/


Jarek Zgoda写道:
Jarek Zgoda wrote:

Chris Lasher napisa3(a):
Chris Lasher napisa3(a):

>我的一个朋友Java和Perl中的编程背景将每个类放在其自己的单独文件中。我告诉他,将所有相关课程保存在一个文件中的内容比Python每个课程中的文件更多。他问为什么,坦率地说,他的有效问题令我感到茫然。

我试图通过声称一个文件 - 一个单独的
模块来合理化这个Python成语 - 一个很好的代码容器,逻辑上绑在一起,比如一个类及其子类。他认为
目录(包)可以将文件绑定在一起,并且通过将类作为单独的文件,他可以差异。他们看看他们有多么不同,他们无法用一个文件中的代码完成任务。
>A friend of mine with a programming background in Java and Perl places
each class in its own separate file in . I informed him that keeping
all related classes together in a single file is more in the Python
idiom than one file per class. He asked why, and frankly, his valid
question has me flummoxed.

I tried to rationalize this Python idiom by claiming a file--a single
module--makes for a great container of code which is logically tied
together, such as a class and its subclasses. He posited that
directories (packages) can tie the files together just as well, and by
having the classes as separate files, he can "diff" them to see how
they differ, something he wouldn''t be able to do with the code all in
one file.



我不会尝试合理化它,这很自然。

的要求每个模块只需要一个公共类需要合理化(除了编译器的b $ b限制),而不是在
中拥有尽可能多的类的自由
模块是程序'architecure所需要的。


I''d not try to rationalize it, it''s just natural. The requirement to
have only one public class per module needs rationalization (other than
limitation of compiler), not the freedom to have as much of classes in
module as is needed by program''s architecure.



您的朋友发现能够将他的课程差异化的事实

意味着他无论如何都没有采用模块化的优势,而是使用复制粘贴技术来代替
。当然我可能会给他一个

的伤害。

在Java *中,所有*都必须在类中,与Python不同。模块

对我来说似乎是一个更自然的存储单元,并且强迫我们把每个类放在自己的模块中看起来不自然。

问候

Steve

-

Steve Holden +44 150 684 7255 +1 800 494 3119 >
Holden Web LLC / Ltd http://www.holdenweb.com

Skype:holdenweb http://del.icio.us/ steve.holden

最近的Ramblings http://holdenweb.blogspot .com

The fact that your friend find being able to diff his classes valuable
implies that he isn''t taking much advantage of modularity anyway but
instead using copy-and-paste techniques. Of course I may be doing him a
disservice.

In Java *everything* has to be in a class, unlike in Python. The module
seems a much more natural unit of storage to me, and to force us to put
each class in its own module would seem unnatural.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
Recent Ramblings http://holdenweb.blogspot.com


Chris Lasher schrieb:
Chris Lasher schrieb:

为什么我们不应该

将课程放在他们自己独立的文件中?


非常感谢的想法,评论和见解,
why should we NOT be
placing classes in their own separate files?

Thoughts, comments, and insight much appreciated,



起初:如果他真的喜欢它,他可以将每个班级放在一个

文件中。但是有一些原因可以解释为什么Python允许你可以在一个文件中放置许多

类:


- 它(更快),不需要额外的文件系统查找。 ;)

- 您可以在类中定义一个类。例如,Django将其用于

它的数据模型。如果你这样做,你被迫有多个班级

存档。示例:
http://www.djangoproject.com/documen...e-in-the-admin


参见:
http://mail.python.org/pipermail/pyt ...... er / 418363.html


Thomas

At first: if he really like it he can place every class in a single
file. But there are some reasons why Python "allows" you to place many
classes in one file:

- It''s (a little bit) faster, no additional file system lookup is needed. ;)
- You can define a class in a class. Django, for example, uses this for
it''s data models. If you do this you are forced to have multiple classes
in on file. Example:
http://www.djangoproject.com/documen...e-in-the-admin

See also:
http://mail.python.org/pipermail/pyt...er/418363.html

Thomas