pep

continuation line missing indentation or outdented 这个坑爹的校验也是没谁了

▼魔方 西西 提交于 2019-12-01 13:41:01
PyCharm配置autopep8,自动格式化Python代码 1. 关于PEP 8 PEP 8,Style Guide for Python Code,是 Python 官方推出编码约定,主要是为了保证 Python 编码的风格一致,提高 代码 的可读性。 官网地址: https://www.python.org/dev/peps/pep-0008/ 2. 关于Auto pep8 Auto pep8 是自动将Python 代码 格式化 为符合PEP 8风格的工具。它使用pycodestyle工具来确定代码的哪些部分需要被 格式化 。Auto pep8 能够修复大部分pycodestyle检测的格式问题。 github地址: https://github.com/hhatto/autopep8 3. 下载安装Autopep8 pip install autopep8 4. 使用Autopep8 命令行使用方式如下 $ autopep8 --in-place --aggressive --aggressive 文件名 5. Pycharm配置Autopep8方法 1)选择菜单「File」–>「Settings」–>「Tools」–>「External Tools」–>点击加号添加工具 2)填写如下配置项,点击「OK」保存 Name:Autopep8 (可随意填写) Tools

What's the difference on docstrings with triple SINGLE quotes and triple DOUBLE quotes?

夙愿已清 提交于 2019-11-30 17:57:51
I was just wondering what is the difference between two ways of writing Python Docstrings ( __doc__ ): three single quotes: ''' Comment goes here ''' three double quotes: """ Comment goes here """ Is there any subtle difference in the way doc string could be formatted later while generating docs? No. They are the same. The only difference is that the first one can contain a sequence of three unescaped double quotes, while the second can contain a sequence of three unescaped single quotes. (In other words, because the delimiters are different, there is a slight difference in what characters you

pycharm集成autopep8

北城余情 提交于 2019-11-30 08:38:26
在Pycharm中配置autopep8 关于PEP 8 PEP 8,Style Guide for Python Code,是Python官方推出的Python编码风格的约定,虽然这不是硬性的规定,但是如果Python程序员都尽量遵循这个文档,那么编码风格的统一会让代码的可读性大大提升。通过它,可以修复大部分PEP8工具中报告的代码排版问题。举个官网的例子: def example1(): ####This is a long comment. This should be wrapped to fit within 72 characters. some_tuple=( 1,2, 3,'a' ); some_variable={'long':'Long code lines should be wrapped within 79 characters.', 'other':[math.pi, 100,200,300,9876543210,'This is a long string that goes on'], 'more':{'inner':'This whole logical line should be wrapped.',some_tuple:[1, 20,300,40000,500000000,60000000000000000]}} return (some

Python 3.8测试阶段正式开始,发布Beta 1版

梦想与她 提交于 2019-11-29 07:25:01
上周,Python背后的团队宣布发布了Python 3.8.0b1 版本,这是Python 3.8计划的四个beta发行预览版中的第一个。此版本标志着beta阶段的开始,您可以在此阶段测试新特性,并使您的应用程序为新版本做好准备。 下面是即将发布的Python 3.8版本中的一些特性: 赋值表达式 在经过Python开发人员的广泛讨论之后,在PEP 572中提出了赋值表达式。这个特性引入了一个新的操作符(:=),您可以使用它在表达式中分配变量。 Positional-only参数 在Python中,可以通过位置、关键字或两者同时传递参数给函数。API设计人员有时可能希望仅限制按位置传递参数。为了方便实现这一点,Python 3.8将附带一个新标记(/),以指示其左边的参数仅是位置的。这类似于*,它指示右边的参数仅为关键字。 Python的初始化配置 Python是高度可配置的,但是配置分散在代码中。该版本为Python初始化C API引入了新的函数和结构,为Python开发人员提供了配置Python的“简单而可靠的方法”。 CPython的Vectorcall协议 增强功能引入了代码的灵活性和性能。为了优化对象的调用,本版本引入了Vectorcall协议和一个已在内部用于Python和内置函数的调用约定。 运行时审计钩子 Python 3.8将提供两个新的api: Audit

How can I use setuptools to generate a console_scripts entry point which calls `python -m mypackage`?

邮差的信 提交于 2019-11-28 07:35:02
I am trying to be a good Pythonista and following PEP 338 for my package I plan on deploying. I am also trying to generate my executable scripts upon python setuptools install using setuptools entry_points{'console_scripts': ... } options. How can I use entry_points to generate a binary that calls python -m mypackage (and passes *args, **kwargs) ? Here are a few attempts I have made with no success: setuptools( ... (1) entry_points= {'console_scripts': ['mypkg=mypkg.__main__'],}, (2) entry_points= {'console_scripts': ['mypkg=mypkg.main'],}, (3) entry_points= {'console_scripts': ['mypkg=python

How / why does Python type hinting syntax work?

我怕爱的太早我们不能终老 提交于 2019-11-27 21:17:29
I have just seen the following example in PEP 484 : def greeting(name: str) -> str: return 'Hello ' + name print(greeting('Martin')) print(greeting(1)) As expected, this does not work in Python 2: File "test.py", line 1 def greeting(name: str) -> str: ^ SyntaxError: invalid syntax However, it works for Python 3: Hello Martin Traceback (most recent call last): File "test.py", line 5, in <module> print(greeting(1)) File "test.py", line 2, in greeting return 'Hello ' + name TypeError: Can't convert 'int' object to str implicitly This was unexpected. It does not really check types yet, as you can

How can I use setuptools to generate a console_scripts entry point which calls `python -m mypackage`?

独自空忆成欢 提交于 2019-11-27 05:42:56
问题 I am trying to be a good Pythonista and following PEP 338 for my package I plan on deploying. I am also trying to generate my executable scripts upon python setuptools install using setuptools entry_points{'console_scripts': ... } options. How can I use entry_points to generate a binary that calls python -m mypackage (and passes *args, **kwargs) ? Here are a few attempts I have made with no success: setuptools( ... (1) entry_points= {'console_scripts': ['mypkg=mypkg.__main__'],}, (2) entry

How / why does Python type hinting syntax work?

落花浮王杯 提交于 2019-11-26 20:36:36
问题 I have just seen the following example in PEP 484: def greeting(name: str) -> str: return 'Hello ' + name print(greeting('Martin')) print(greeting(1)) As expected, this does not work in Python 2: File "test.py", line 1 def greeting(name: str) -> str: ^ SyntaxError: invalid syntax However, it works for Python 3: Hello Martin Traceback (most recent call last): File "test.py", line 5, in <module> print(greeting(1)) File "test.py", line 2, in greeting return 'Hello ' + name TypeError: Can't

Why is there no xrange function in Python3?

早过忘川 提交于 2019-11-26 19:19:13
Recently I started using Python3 and it's lack of xrange hurts. Simple example: 1) Python2: from time import time as t def count(): st = t() [x for x in xrange(10000000) if x%4 == 0] et = t() print et-st count() 2) Python3: from time import time as t def xrange(x): return iter(range(x)) def count(): st = t() [x for x in xrange(10000000) if x%4 == 0] et = t() print (et-st) count() The results are, respectively: 1) 1.53888392448 2) 3.215819835662842 Why is that? I mean, why xrange's been removed? It's such a great tool to learn. For the beginners, just like myself, like we all were at some point

E731 do not assign a lambda expression, use a def

我只是一个虾纸丫 提交于 2019-11-26 12:22:59
问题 I get this pep8 warning whenever I use lambda expressions. Are lambda expressions not recommended? If not why? 回答1: The recommendation in PEP-8 you are running into is: Always use a def statement instead of an assignment statement that binds a lambda expression directly to a name. Yes: def f(x): return 2*x No: f = lambda x: 2*x The first form means that the name of the resulting function object is specifically 'f' instead of the generic '<lambda>'. This is more useful for tracebacks and