什么是真正的模块? [英] What are modules really for?

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

问题描述

我对Python很陌生,但是已经用C ++进行了大量的开发和

Java。


我觉得有一点奇怪的是python是模块的想法。当已经有类,文件和包的想法时,为什么需要这个




在我看来,虽然有一个CAN可以放入很多类文件,为了可读性和可维护性,最好为每个文件放一个类。


然后可以使用文件组创建包和库,每个文件一个

类。


Python似乎允许你将一个类组合在一个文件中并称之为

模块,但是为了什么?


我发现这与mixins相结合,很难找到继承代码的



使用C ++或Java中的单继承,如果你想看看

方法做了什么并且它似乎是继承的,你只需查看

基类'的文件,如果需要,可以递归继承层次结构

,直到找到方法。


使用Python继承的方法可以在许多bas之一e类

和/或mixins并没有特别的逻辑来确定

文件名是什么。


我是遗失了什么?


任何评论都会受到赞赏。


谢谢

Nick Davis

I am very new to Python, but have done plenty of development in C++ and
Java.

One thing I find weird about python is the idea of a module. Why is this
needed when there are already the ideas of class, file, and package?

To my mind, although one CAN put many classes in a file, it is better to
put one class per file, for readability and maintainability.

One can then create packages and libraries, using groups of files, one
class per file.

Python seems to let you group classes together in one file and call it a
module, but what for?

I find that this, combined with mixins, makes it difficult to find out
where code is inherited from.

With single inheritance in C++ or Java, if you wanted to see what a
method did and it appeared to be inherited, you would simply look in the
base class''s file, and if necessary recurse up the inheritance hierarchy
until you found the method.

With Python an inherited method could be in one of many base classes
and/or mixins and there seems no particular logic as to what the
filename would be.

Am I missing something?

Any comments would be appreciated.

Thanks
Nick Davis

推荐答案



" N.Davis" < ND ** @ le.ac.uk>在留言新闻中写道:42 ************** @ le.ac.uk ......


"N.Davis" <nd**@le.ac.uk> wrote in message news:42**************@le.ac.uk...

在我看来,虽然一个CAN可以将多个类放在一个文件中,为了可读性和可维护性,最好为每个文件放一个类。
To my mind, although one CAN put many classes in a file, it is better to
put one class per file, for readability and maintainability.




文件中的零类工作原理也很好;)



Zero classes in a file works well too ;)




Nick>有一点我觉得python很奇怪是一个模块的想法。为什么

尼克>如果已经有类,文件的想法,这是需要的吗

Nick>和包?

模块是命名空间对象,它将一对一映射到一个文件(忽略

新模块的功能)。文件本身只是一个字节集合。

没有将其内容结构解释为命名空间的功能。


Nick> Python似乎允许您将类组合在一个文件中并且

Nick>称之为模块,但是为什么?

除了类之外,您还可以在模块中定义许多其他类型的对象。


Nick>使用Python,一个继承的方法可以在众多基础之一

Nick>类和/或mixins似乎没有特别的逻辑

Nick>文件名是什么。


我同意mixins可能很麻烦。这是多个

继承特有的问题,而不是Python的严格要求。我相信你可以用Java或C ++创建文件/类

关系,这使得找到mixin的定义很难。我不认为多重继承是

OOP的必备功能,并且尽可能避免它。几十年来,Smalltalk幸福地生活,没有多次继承。


Skip

Nick> One thing I find weird about python is the idea of a module. Why
Nick> is this needed when there are already the ideas of class, file,
Nick> and package?

A module is a namespace object that maps one-to-one to a file (ignoring the
new module''s features). A file by itself is just a collection of bytes and
has no features to interpret the structure of its contents as a namespace.

Nick> Python seems to let you group classes together in one file and
Nick> call it a module, but what for?

You can define many other types of objects in modules besides classes.

Nick> With Python an inherited method could be in one of many base
Nick> classes and/or mixins and there seems no particular logic as to
Nick> what the filename would be.

I agree mixins can be troublesome. That''s a problem peculiar to multiple
inheritance, not strictly to Python. I''m sure you can create file/class
relationships in Java or C++ that make it challenging to find the definition
of a mixin. I don''t view multiple inheritance as a gotta-have feature of
OOP and avoid it when I can. Smalltalk has lived happily without multiple
inheritance for a few decades now.

Skip


> ;我是Python的新手,但在C ++和
> I am very new to Python, but have done plenty of development in C++ and
Java中做了大量的开发。


我认为这就是你问题的根源。

我觉得python很奇怪的是一个模块的想法。当已经有类,文件和包的想法时,为什么需要这个?


一个原因是功能也需要存在的地方。在Java中,每个

函数,甚至是静态函数。必须是一种类方法。在Python中,

" static"功能最好保存在模块中。另一件事是

包是以后添加的语言。

在我看来,虽然一个CAN把很多类放在一个文件中,但最好是
为每个文件放一个类,以提高可读性和可维护性。


就个人而言,当我们在同一个文件中时,我发现维护一组相关课程更容易。我有几个模块,我是目前正在攻击的
,每个模块都包含一些课程。

也许这只是一个问题规模,因为这些都是相当小的
库,但我没有看到任何优势将它们分成多个文件



然后可以使用文件组创建包和库,每个文件一个类。


由于Java的编译器强制执行此操作,或许你只是接受

它为正常。

Python似乎允许你将类组合在一个文件中并将其称为
模块,但是为什么?


如果您的某个类创建/操作其他类,该怎么办?如果

他们在同一个模块中,那么它们都存在于同一个命名空间中

并且你不必让模块相互导入或者这样的东西。

我发现这与mixins相结合,很难找到继承代码的地方。


也许你过分依赖继承,那么?

如果你想用C ++或Java进行单继承,那么你是否想看看是什么?
方法做了,它似乎是继承的,你只需查看
基类的文件,并在必要时递归继承层次结构
直到找到方法。
和/或mixins中,似乎没有特定的逻辑来确定
文件名。


除非有人在某种程度上含糊不清,否则不应该太难理解。你总是可以启动翻译,导入你的班级,然后检查它的.mro属性(方法解析顺序),

列出顺序中的类他们将被检查以找到在运行时命名的

方法。

我错过了什么?
Java.
And therein lies the root of your question, I believe.
One thing I find weird about python is the idea of a module. Why is this
needed when there are already the ideas of class, file, and package?
One reason is that functions need a place to exist too. In Java, every
function, even "static" ones has to be a class method. In Python,
"static" functions are best kept in a module. Another thing is that
packages were a later addition to the language.
To my mind, although one CAN put many classes in a file, it is better to
put one class per file, for readability and maintainability.
Personally I find it easier to maintain a set of related classes when
they''re all in the same file. I''ve got a few modules that I''m
currently hacking on, each of which contains a handful of classes.
Maybe it''s just a matter of scale, since these are both fairly small
libraries, but I just don''t see any advantage to splitting them up into
multiple files.
One can then create packages and libraries, using groups of files, one
class per file.
Since Java''s compiler enforces this, perhaps you''ve just come to accept
it as "normal".
Python seems to let you group classes together in one file and call it a
module, but what for?
What if one of your classes creates/manipulates other classes. If
they''re in the same module then they all exist in the same namespace
and you don''t have to have modules importing each other or such things.
I find that this, combined with mixins, makes it difficult to find out
where code is inherited from.
Perhaps you are relying too much on inheritance, then?
With single inheritance in C++ or Java, if you wanted to see what a
method did and it appeared to be inherited, you would simply look in the
base class''s file, and if necessary recurse up the inheritance hierarchy
until you found the method.

With Python an inherited method could be in one of many base classes
and/or mixins and there seems no particular logic as to what the
filename would be.
It shouldn''t be too hard to figure out, unless someone was being
intentially vague. You could always fire up the interpreter, import
your class, and check it''s .mro property (method resolution order),
which lists the classes in the order they will be examined to find a
method named at runtime.
Am I missing something?




我只是认为你在思考Java。你会快速拿起

但是:-)



I just think you''re thinking in terms of Java. You''ll pick things up
quickly, though :-)


这篇关于什么是真正的模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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