python3 怎么设置代码格式_设置 Python 代码格式

设置 Python 代码格式Format Python code

03/13/2019

本文内容

在 Visual Studio 中,可以快速重新设置代码格式以匹配预配置的格式设置选项。Visual Studio lets you quickly reformat code to match pre-configured formatting options.

若要设置选定内容的格式:请选择“编辑” > “高级” > “设置选定内容的格式”或按 Ctrl+E > F。To format a selection: select Edit > Advanced > Format Selection or press Ctrl+E > F.

若要设置整个文件的格式:请选择“编辑” > “高级” > “设置文档的格式”或按 Ctrl+E > D。To format the whole file: select Edit > Advanced > Format Document or press Ctrl+E > D.

可通过“工具” > “选项” > “文本编辑器” > “Python” > “格式”及其嵌套选项卡来设置选项。Options are set through Tools > Options > Text Editor > Python > Formatting and its nested tabs. 需要选择“显示所有设置” 来显示这些选项:You need to select Show all settings for these options to appear:

默认情况下,“格式”选项设置为匹配 PEP 8 样式指南的超集。Formatting options by default are set to match a superset of the PEP 8 style guide. “常规” 选项卡确定何时应用格式;本文介绍了其他三个选项卡的设置。The General tab determines when formatting is applied; settings for the other three tabs are described in this article.

Python support in Visual Studio also adds the useful Fill Comment Paragraph command to the Edit > Advanced menu as described in a later section.

间距Spacing

间距控制各种语言构造周围插入或删除空格的位置。Spacing controls where spaces are inserted or removed around various language constructs. 每个选项都有三个可能值:Each option has three possible values:

已选中:确保间距已应用。Checked: ensures the spacing is applied.

清除:删除所有间距。Cleared: removes any spacing.

不确定:保留原始格式。Indeterminate: leaves original formatting in place.

下表提供关于各种选项的示例:Examples for the various options are provided in the following tables:

类定义选项Class definitions option

已选中Checked

清除Cleared

在类定义名称和基列表之间插入空格Insert space between a class declaration's name and bases list

class X (object): pass

class X(object): pass

在基列表括号内插入空格Insert space within bases list parentheses

class X( object ): pass

class X(object): pass

在空的基列表括号内插入空格Insert space within empty bases list parentheses

class X( ): pass

class X(): pass

函数定义选项Function definitions option

已选中Checked

清除Cleared

在函数声明名称和参数列表间插入空格Insert space between a function declaration's name and parameter list

def X (): pass

def X(): pass

在参数列表括号内插入空格Insert space within parameter list parentheses

def X( a, b ): pass

def X(a, b): pass

在空的参数列表括号内插入空格Insert space within empty parameter list parentheses

def X( ): pass

def X(): pass

在默认参数值中的“=”周围插入空格Insert spaces around '=' in default parameter values

includes X(a = 42): pass

includes X(a=42): pass

在返回批注运算符前后插入空格Insert space before and after return annotation operators

includes X() -> 42: pass

includes X()->42: pass

运算符选项Operators option

已选中Checked

清除Cleared

在二元运算符周围插入空格Insert spaces around binary operators

a + b

a+b

在赋值周围插入空格Insert spaces around assignments

a = b

a=b

表达式间距选项Expression spacing option

已选中Checked

清除Cleared

在函数调用名称和参数列表间插入空格Insert space between a function call's name and argument list

X ()

X()

在空参数列表的括号中插入空格Insert space within empty argument list parentheses

X( )

X()

在参数列表的括号中插入空格Insert space within argument list parentheses

X( a, b )

X(a, b)

在表达式的括号内插入空格Insert space within parentheses of expression

( a )

(a)

在空的元组括号内插入空格Insert space within empty tuple parentheses

( )

()

在元组括号内插入空格Insert space within tuple parentheses

( a, b )

(a, b)

在空方括号中插入空格Insert space within empty square brackets

[ ]

[]

在列表的方括号内插入空格Insert spaces within square brackets of lists

[ a, b ]

[a, b]

在左方括号前插入空格Insert space before open square bracket

x [i]

x[i]

在方括号中插入空格Insert space within square brackets

x[ i ]

x[i]

语句Statements

“语句”选项用于控制以其他更 Python 的形式自动重写各种语句 。The Statements options control automatic rewriting of various statements into more Pythonic forms.

选项Option

设置格式前Before formatting

设置格式后After formatting

将导入的模块置于新行Place imported modules on new line

import sys, pickle

import sys

import pickle

移除不必要的分号Remove unnecessary semicolons

x = 42;

x = 42

将多个语句置于新行Place multiple statements on new lines

x = 42; y = 100

x = 42

y = 100

换行Wrapping

“换行”选项用于设置“最大注释宽度”(默认值为 80) 。Wrapping lets you set the Maximum comment width (default is 80). 设置“对过宽的注释换行”选项后,Visual Studio 会重新设置注释格式,使其不超过该最大宽度 。If the Wrap comments that are too wide option is set, Visual Studio reformats comments to not exceed that maximum width.

# Wrapped to 40 columns

# There should be one-- and preferably

# only one --obvious way to do it.

# Not-wrapped:

# There should be one-- and preferably only one --obvious way to do it.

填充注释段落命令Fill Comment Paragraph command

使用“编辑” > “高级” > “填充注释段落”(Ctrl+E > P) 可重排并设置注释文本格式,从而组合短行、分割长行。Edit > Advanced > Fill Comment Paragraph (Ctrl+E > P) reflows and formats comment text, combining short lines together and breaking up long ones.

例如:For example:

# foo

# bar

# baz

更改为:changes to:

# foo bar baz

# This is a very long long long long long long long long long long long long long long long long long long long comment

更改为:changes to:

# This is a very long long long long long long long long long long long long

# long long long long long long long comment

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值