简体   繁体   English

(自动)在Pycharm / Python中的=后缩进

[英](Auto)-indent after the = in Pycharm/Python

I am relatively new to Python/Pycharm coming from a Matlab background. 我对Matlab背景的Python / Pycharm比较陌生。 I noticed one thing that I am unable to reproduce in Pycharm/Python. 我注意到我无法在Pycharm / Python中复制的一件事。

When I write code as follows: 当我编写如下代码时:

albert = 1
b      = 2

And copy/paste it to a new file, I get: 并将其复制/粘贴到新文件中,我得到:

albert = 1
b = 2

The alignment = disappears and everything gets cluttered to my opinion. 对齐=消失,一切都变得混乱起来。 As I am used to the alignment, I am having a hard time getting a quick overview of the variables and their values, in particular when the variable names are long. 习惯了对齐之后,我很难快速了解变量及其值,尤其是当变量名很长时。

How can I prevent this and also remove the warning? 如何防止这种情况并删除警告? Why is it like this in the first place, I find it gets easily messy if you do not align your = for multiple statements? 首先为什么会这样,如果您不对多个语句对齐=,我会发现它很容易变得混乱。

In addition I was wondering if there is an auto-indent command like in Matlab (ctrl+R) to indent all the code properly. 另外,我想知道是否有像Matlab(ctrl + R)这样的自动缩进命令来正确缩进所有代码。

In Python, multilines alignments are mostly used to point out split lines. 在Python中,多行对齐方式通常用于指出分割线。 For example : 例如 :

print('my first sentence',
      'my second sentence')

This is, I guess, one of the reasons it is not recommended to do it the Matlab way. 我猜这是不建议您使用Matlab方法进行操作的原因之一。 Also, as far as I know, it is fairly more common to do it this way than the matlab way. 而且,据我所知,用这种方法比用matlab方法更普遍。

As for Pycharm, I'm not sure you can make it force this kind of coding style, but you could start by deactivate the "Assignment operators" rules in Settings -> Editor -> Code Style -> Python -> Spaces. 至于Pycharm,我不确定您是否可以使其强制执行这种编码样式,但是您可以先停用设置->编辑器->代码样式-> Python->空格中的“赋值运算符”规则。

The warning is easy to deactivate : in Settings -> Editor -> Inspections -> Python -> PEP 8 coding style violation, you need to ignore error E225. 该警告很容易停用:在设置->编辑器->检查-> Python-> PEP 8编码样式冲突中,您需要忽略错误E225。

Do one of the following: 请执行以下任一操作:

  • On the main menu, choose Edit | Paste Simple. 在主菜单上,选择“ Edit | Paste Simple. Edit | Paste Simple.

  • Press Ctrl+Shift+Alt+V . Ctrl + Shift + Alt + V。

This way, it will paste it without any additional formatting. 这样,它将粘贴它而没有任何其他格式。

Python developers have some rule, even if it is not explicitly written. Python开发人员有一些规则,即使未明确编写也是如此。 Without these rules, it would be complicated to read a code which is not from you. 如果没有这些规则,则读取非您提供的代码会很复杂。 One of the rule is that when you declare your variable b, there is a space before the '=' and after the '=', and pycharm is doing that for you. 规则之一是,当您声明变量b时,在'='之前和'='之后有一个空格,而pycharm会为您这样做。 You can find a style guide that explain all of the rules : https://www.python.org/dev/peps/pep-0008/ 您可以找到解释所有规则的样式指南: https : //www.python.org/dev/peps/pep-0008/

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

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