pep8

What does '# noqa' mean in Python comments?

好久不见. 提交于 2019-11-27 05:33:58
问题 While searching through a Python project, I found a few lines commented with # noqa . import sys sys.path.append(r'C:\dev') import some_module # noqa What does noqa mean in Python? Is it specific to Python only? 回答1: Adding # noqa to a line indicates that the linter (a program that automatically checks code quality) should not check this line. Any warnings that code may have generated will be ignored. That line may have something that "looks bad" to the linter, but the developer understands

Why does PEP-8 specify a maximum line length of 79 characters? [closed]

本秂侑毒 提交于 2019-11-27 04:59:25
问题 Why in this millennium should Python PEP-8 specify a maximum line length of 79 characters? Pretty much every code editor under the sun can handle longer lines. What to do with wrapping should be the choice of the content consumer, not the responsibility of the content creator. Are there any (legitimately) good reasons for adhering to 79 characters in this age? 回答1: Much of the value of PEP-8 is to stop people arguing about inconsequential formatting rules, and get on with writing good,

How to properly use python's isinstance() to check if a variable is a number?

我们两清 提交于 2019-11-27 00:56:32
问题 I found some old Python code that was doing something like: if type(var) is type(1): ... As expected, pep8 complains about this recommending usage of isinstance() . Now, the problem is that the numbers module was added in Python 2.6 and I need to write code that works with Python 2.5+ So if isinstance(var, Numbers.number) is not a solution. Which would be the proper solution in this case? 回答1: In Python 2, you can use the types module: >>> import types >>> var = 1 >>> NumberTypes = (types

How to integrate pep8.py in Eclipse?

穿精又带淫゛_ 提交于 2019-11-26 19:04:32
问题 A little background: PEP 8 is the Style Guide for Python Code . It contains the conventions all python programmers should follow. pep8.py is a (very useful) script that checks the code formating of a given python script, according to PEP 8. Eclipse is a great IDE. With the Pydev extension, it that can be used to develop Python I run pep8.py manually when I'm scripting, but with bigger projects I prefer to use Eclipse. It would be really useful to integrate pep8.py in Eclipse/Pydev, so it can

Tool to convert Python code to be PEP8 compliant

断了今生、忘了曾经 提交于 2019-11-26 18:43:29
问题 I know there are tools which validate whether your Python code is compliant with PEP8, for example there is both an online service and a python module. However, I cannot find a service or module which can convert my Python file to a self-contained, PEP8 valid Python file. Does anyone know if there are any? I assume it's feasible since PEP8 is all about the appearance of the code, right? 回答1: Unfortunately "pep8 storming" (the entire project) has several negative side-effects: lots of merge

Strange PEP8 recommandation on comparing Boolean values to True or False

风流意气都作罢 提交于 2019-11-26 16:54:52
问题 At the end of python PEP8 I'm reading: Don't compare boolean values to True or False using == Yes: if greeting: No: if greeting == True: Worse: if greeting is True: I have no problem with that recommandation when the boolean is True , but it sounds strange when checking for False If I want to know if variable greeting is False why shouldn't I write: if greeting == False: If I write if not greeting: it will have a very different meaning that the above statement. What if greeting is None ? What

How to break a line of chained methods in Python?

天大地大妈咪最大 提交于 2019-11-26 15:56:39
I have a line of the following code (don't blame for naming conventions, they are not mine): subkeyword = Session.query( Subkeyword.subkeyword_id, Subkeyword.subkeyword_word ).filter_by( subkeyword_company_id=self.e_company_id ).filter_by( subkeyword_word=subkeyword_word ).filter_by( subkeyword_active=True ).one() I don't like how it looks like (not too readable) but I don't have any better idea to limit lines to 79 characters in this situation. Is there a better way of breaking it (preferably without backslashes)? You could use additional parenthesis: subkeyword = ( Session.query(Subkeyword

Line continuation for list comprehensions or generator expressions in python

天涯浪子 提交于 2019-11-26 15:29:19
问题 How are you supposed to break up a very long list comprehension? [something_that_is_pretty_long for something_that_is_pretty_long in somethings_that_are_pretty_long] I have also seen somewhere that people that dislike using '\' to break up lines, but never understood why. What is the reason behind this? 回答1: [x for x in (1,2,3) ] works fine, so you can pretty much do as you please. I'd personally prefer [something_that_is_pretty_long for something_that_is_pretty_long in somethings_that_are

Python: using 4 spaces for indentation. Why? [closed]

末鹿安然 提交于 2019-11-26 15:25:17
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 5 years ago . While coding python I'm using only 2 spaces to indent, sure PEP-8 really recommend to have 4 spaces, but historically for me it's unusual. So, can anyone convince me to use 4 spaces instead of 2? What pros and cons? P.S. And finally, what's easy way to convert all existing

How to write very long string that conforms with PEP8 and prevent E501

元气小坏坏 提交于 2019-11-26 15:09:26
问题 As PEP8 suggests keeping below the 80 column rule for your python program, how can I abide to that with long strings, i.e. s = "this is my really, really, really, really, really, really, really long string that I'd like to shorten." How would I go about expanding this to the following line, i.e. s = "this is my really, really, really, really, really, really" + "really long string that I'd like to shorten." 回答1: Implicit concatenation might be the cleanest solution: s = "this is my really,