简体   繁体   English

遵循python google-style-guide时避免大文件

[英]Avoiding huge files when following python google-style-guide

Google style guide requires to import modules.谷歌风格指南需要导入模块。 Say I have a non trivial class and several non-trivial derived classes.假设我有一个非平凡的类和几个非平凡的派生类。 It seems that I have to put them all in the one file otherwise will have to use different prefixes when using them.似乎我必须将它们全部放在一个文件中,否则在使用它们时将不得不使用不同的前缀。

Personally I prefer one class per file.我个人更喜欢每个文件一个类。 Is there a common idiom to avoid unnecessary large files?是否有一个常见的习惯用法来避免不必要的大文件?

Eg can I rename several imports into one as long as there is no name conflicts?例如,只要没有名称冲突,我可以将多个导入重命名为一个吗?

import data_main as data
import data_derived_a as data
import data_derived_b as data

With modules, no, there's really no way of doing this that I know of.使用模块,不,我所知道的真的没有办法做到这一点。 If you're happy to put your class in separate files within a package, though, you can import the individual classes in __init__.py , and your client modules can import them all at once, like so.但是,如果您愿意将您的类放在一个包中的单独文件中,您可以在__init__.py导入各个类,并且您的客户端模块可以像这样一次性导入它们。

# mypackage/a.py
class A:
    pass

# mypackage/b.py
class B:
    pass

# mypackage/__init__.py
from .a import A
from .b import B

# mymodule.py
from mypackage import A, B
a = A()
b = B()

# Or mymodule.py
import mypackage as p
a = p.A()
b = p.B()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM