本文介绍了初始化中的python pep8类已导入但未使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用python flake8 库在python中进行PEP8检查.我的子模块之一的__init__.py文件中有一个导入语句,如下所示:

I'm doing PEP8 checks in python using the python flake8 library. I have an import statement in an __init__.py file in one of my sub-modules which looks like this:

from .my_class import MyClass

我在init文件中包含此行的原因是,这样我就可以将子类的MyClass作为from somemodule import MyClass导入,而不必编写from somemodule.my_class import MyClass.

The reason I have this line in the init file is so that I can import MyClass from the sub-module as from somemodule import MyClass instead of having to write from somemodule.my_class import MyClass.

我想知道在纠正PEP8违规时是否可以维持此功能?

I would like to know if it is possible to maintain this functionality while correcting the PEP8 violation?

推荐答案

这实际上不是PEP8违规.我只是这样做:

This is not actually a PEP8 violation. I simply do this:

from .my_class import MyClass  # noqa

另一种可能性是使用__all__.在这种情况下,flake8会了解发生了什么:

Another possibility is to use __all__. In that case, flake8 understands what is going on:

from .my_class import MyClass

__all__ = ['MyClass',]

这篇关于初始化中的python pep8类已导入但未使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-12 00:54