组织代码 - 包 [英] Organizing Code - Packages

查看:51
本文介绍了组织代码 - 包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

全部,


如果这是一个常见问题我道歉,但我没有找到任何在搜索时回答我问题的内容。


所以我现在所拥有的是一些包含一些常用

使用函数的软件包以及另一个包含我所有自定义的软件包

错误类。我希望这些错误类可以在我的库中的其他软件包中使用

。目前为了在每个模块文件的顶部

实现这一点,我有一行来自My.Library.Errors import

*,我的问题是它设法在每个使用的范围内将错误导入

。我仍然是Python的新手,而且我的

方法可能非常根植于C / C ++(我已经度过了最难的时间

能够超载功能),但我正在做这个吗?

这是正确的吗?


此外,是否有任何好的教程/示例如何

将您的python代码组织成包装?


感谢所有帮助!


问候,

Ken

All,

I apologize if this is a commonly asked question, but I didn''t
find anything that answered my question while searching.

So what I have right now is a few packages that contain some commonly
used functions and another package that contains all of my custom
error classes. I want these error classes available to me in all of
the other packages in my library. Currently to achieve this at the top
of every module file I have the line "from My.Library.Errors import
*", my problem with this is that it manages to import the Errors into
every scope that they are used. I''m still pretty new to Python, and my
approachs are probably very rooted in C/C++ (I''ve had the hardest time
getting over not being able to overload functions), but am I doing
this correctly?

Also, are there any good tutorials/examples out there of how to
organize your python code into packges?

Thanks for all the help!

Regards,
Ken

推荐答案

xkennethécrit:
xkenneth a écrit :

全部,


如果这是一个常见问题,我道歉,但我没有找到任何在搜索时回答我问题的东西。


所以我现在所拥有的是一些包含一些通常

使用函数的包和另一个包含我所有自定义

错误类的包。我希望这些错误类可以在我的库中的其他软件包中使用

。目前为了在每个模块文件的顶部

实现这一点,我有一行来自My.Library.Errors import

*,我的问题是它设法在每个使用的范围内将错误导入


All,

I apologize if this is a commonly asked question, but I didn''t
find anything that answered my question while searching.

So what I have right now is a few packages that contain some commonly
used functions and another package that contains all of my custom
error classes. I want these error classes available to me in all of
the other packages in my library. Currently to achieve this at the top
of every module file I have the line "from My.Library.Errors import
*", my problem with this is that it manages to import the Errors into
every scope that they are used.



不管你想要什么?在我的库中的所有其他软件包中都有这些错误类可供我使用




如果你担心穿孔或其他什么,不用担心,一个模块只需要进口一次
- 下一次导入只会绑定导入

名称空间中的名称。

Ain''t that what you want ??? Having "these error classes available to me
in all of the other packages in my library" ?

If you''re worried about perfs or whatever, don''t worry, a module is only
imported once - next imports will only bind the names in the importing
namespace.


我仍​​然是Python的新手,我的

方法可能非常根植于C / C ++(我已经度过了最难的时间

越过不能重载功能),但我在做什么

这个是否正确?
I''m still pretty new to Python, and my
approachs are probably very rooted in C/C++ (I''ve had the hardest time
getting over not being able to overload functions), but am I doing
this correctly?



是的,这是正确的做法。

Yes, that''s the right thing to do.


此外,还有什么好处教程/示例如何将
组织你的python代码到包装中?
Also, are there any good tutorials/examples out there of how to
organize your python code into packges?



大多数特定于Python的方面应该由教程

(文档中的那个)涵盖。另外,它和往常一样,试图拥有高凝聚力

和低耦合。


啊,是的,有几件事:
- 避免使用''每个文件一级''综合症。在同一个模块中有几十个类可以完全没问题

- 普通函数也可以 - 不需要在课堂上粘贴所有东西。


HTH

Most of the Python-specific aspects should be covered by the tutorial
(the one in the doc). Else, it''s as usual, trying to have high cohesion
and low coupling.

Ah, yes, a couple of things:
- avoid the ''one-class-per-file'' syndrom. It''s perfectly ok to have tens
of classes in a same module
- plain functions are ok too - no need to stick everything in classes.

HTH



啊,是的,有几件事:

- 避免使用''one-class-per-file''综合症。有几十美元b $ b
Ah, yes, a couple of things:
- avoid the ''one-class-per-file'' syndrom. It''s perfectly ok to have tens



是完全可以的但是当我有超过

一类时,我发现很难轻松编辑课程每个文件。


问候,



Yes but i find it hard to edit classes easily when I have more than
one class per file.

Regards,
Ken


xkenneth< xk * *****@gmail.comwrites:
xkenneth <xk******@gmail.comwrites:

>啊,是的,有几件事:
- 避免''每个文件一级''综合症。有几十美元是完全没问题b $ b
>Ah, yes, a couple of things:
- avoid the ''one-class-per-file'' syndrom. It''s perfectly ok to have tens



是的,但是当我有超过

的课程时,我觉得很难轻松编辑课程每个文件。


Yes but i find it hard to edit classes easily when I have more than
one class per file.



为什么?

Why?


这篇关于组织代码 - 包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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