PyCharm - 作者是否在进口之前出现? [英] PyCharm - Have author appear before imports?

查看:138
本文介绍了PyCharm - 作者是否在进口之前出现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当您创建新的python文件并添加新的导入时,PyCharm会自动添加导入和__author__标记。但是,默认情况下,__ author__标记将始终显示在任何导入下方。在我看来,__ author__标签应该在文件的顶部,我也会放置像docstrings这样的东西。这样,描述文件的所有内容都位于顶部,然后实际代码(包括导入)低于该值。

When you create new python files and add new imports, PyCharm will automatically add the imports and __author__ tag whenever it can by itself. However, by default the __author__ tag will always appear below any imports. It seems to me that the __author__ tag should be up at the top of the file where I would also put things like docstrings. This way everything describing the file is at the top, then the actual code (including the imports) is below that.

所以有两个问题:


  1. 是否有充分理由将__author__标记放在导入之下?

  2. 如何设置PyCharm以放置__author__标记默认情况下进口以上?


推荐答案


  1. 根据进口 PEP-8 的部分:


导入总是放在文件的顶部,就在任何模块注释和文档字符串之后,模块全局变量和常量之前

Imports are always put at the top of the file, just after any module comments and docstrings, and before module globals and constants

__ author __ 是一个全局变量,因此应该出现在导入之下。

__author__ is a global "variable" and should therefore appear below the imports.

您可以转到Pycharm的设置(Ctrl-Alt-S),选择文件和代码模板并根据自己的喜好调整Python脚本模板。

You may go to Pycharm's settings (Ctrl-Alt-S), choose "File and Code Templates" and adjust the "Python Script" template to your liking.

请注意:正如下面Martijn的评论所述,上述声明的第1项不再适用于PEP8已更新(2016年6月) https://www.python.org / dev / peps / pep-0008 /#id24 有一个很好的例子

Pls Note: As mentioned by Martijn's comment below, item 1 of the above statement is no longer true as PEP8 has been updated (in June 2016) https://www.python.org/dev/peps/pep-0008/#id24 with a good example

"""
This is the example module.
This module does stuff.
"""

from __future__ import barry_as_FLUFL

__all__ = ['a', 'b', 'c']
__version__ = '0.1'
__author__ = 'Cardinal Biggles'

import os
import sys

这篇关于PyCharm - 作者是否在进口之前出现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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