Python命名空间和类 [英] Python namespacing and classes

查看:142
本文介绍了Python命名空间和类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Python中写一些(在我看来)可读的代码。我需要一个模块,将包含一些类。理论上我知道完成这一切需要的一切:我可以简单地把类定义放在一个单一的模块文件。

I'm trying to write some (in my opinion) readable code in Python. I need a module that will contain a number of classes. Theoretically I know everything that is needed to accomplish this: I can simply put class definitions in a single module file.

出于可读性的目的,我想把每个类定义分开文件(他们开始相当冗长!),并将所有这些类放入一个目录。每当我创建新文件,虽然它的内容是可见的,我需要它们,类定义在里面是不必要的模块。

For readability purposes I want to put every class definition into separate file (they are starting to be quite lengthy!), and all these classes into one directory. Whenever I create new file although it's contents are visible where I need them, class definition that's inside is scoped with unneeded module.

我该怎么办?

推荐答案

将它们全部导入 __ init __。py

Import them all within __init__.py, and then have the consumer import the package.

from .module1 import Class1
from .module2 import Class2
 ...

这篇关于Python命名空间和类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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