您会忽略哪些PEP 8准则,并坚持遵循哪些准则? [英] What PEP 8 guidelines do you ignore, and which ones do you stick to?

查看:128
本文介绍了您会忽略哪些PEP 8准则,并坚持遵循哪些准则?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

多年来,尽管我出于自己的原因始终如一地故意破坏某些准则,但我编写的Python越多,我就越同意大多数准则.

Over the years, the more Python I write, the more I find myself agreeing with most of the guidelines, though I consistently and intentionally break some for my own reasons.

我很想知道人们在宗教信仰8中(或也许还有其他的人民教义)坚持什么信仰,为什么这样做,以及人们感到不便或不足的地方.

I'd be curious to know what in PEP 8 (or other PEPs too maybe) people religiously stick to and why, and what people find inconvenient or inadequate.

就我而言(以及在一般情况下工作),我们只有几处不同之处:

In my case (and at work in general), there's only a handful of things we deviate from:

  • 用下划线分隔小写字母的名称,我可以看到它的含义,因为它始终是一致的,但是我们倾向于使用lowerCamelCase,即使它偶尔会引入一些不一致的地方(例如部分或大写的首字母缩写词也是如此)以及紧随其后的字词,而这些字词通常都是一时冲动而来的).主要是因为我们通常使用的API几乎全部使用camelCase(有些较高,有些较低),并且由于某种原因我发现它更易于阅读,并且倾向于将下划线保留为分隔标记或规定的修饰/遮盖.

  • Underscore separated lowercase names, I can see the point of it, as it will unfailingly be consistent, but we tend to use lowerCamelCase, even if it will occasionally introduce some inconsistencies (such as partially or mis-capitalized acronyms and following words, which are often down to spur-of-the-moment calls). Mostly because the near totality of the APIs we routinely use use camelCase (some upper, some lower), and because for some reason I find it easier to read, and tend to reserve underscores as separation tokens or prescribed mangling/obscuring.

我仍然无法让自己按照PEP在对象内部开出的方式来分隔事物. new和init,我倾向于直接留在类下面,没有空行,因为我总是想在那里用类名和args读取它们,这些方法在类中具有相同的功能范围(例如init,get和set相同的属性或一组属性),我仅相隔一个空格,并且我喜欢类之间的三个空格,而方法之间的两个空格我不会在该对象的映射中进行脑力聚集. 再次,这纯粹是出于代码的视觉效果和可读性.我发现流控制中非常紧凑的内容以及方法和对象之间的这种间距始终使我的眼睛准确地看到我希望代码在停放几个月后进行重新读取的位置.对我选择的编辑器中的折叠效果也很好.

I still can't get myself to space things out the way the PEP prescribes inside objects. new and init I tend to leave right under the class with no blank lines as I always want to read them right there with the class name and args, methods that contribute to the same scope of functionality in the class (say init, get and set of the same attrib or set of attribs) I only single-space apart, and I like three spaces between classes, and two between methods I wouldn't mentally aggregate in the map of that object. This is, again, purely for the visual impact and readability of the code. I find that very compact contents inside flow control and this kind of spacing between methods and objects consistently leads my eye exactly where I want it to go on re-readings months after the code had been parked. It also responds well to folding in my editors of choice.

有些我反而坚持使用的东西,当我以其他方式阅读时,让我很生气的是选项卡而不是空格(特别是当我们使用的某些应用内编辑器实际上没有选项卡替换功能时,原型阶段代码库中的污染.)

Some things instead I stick to, that drive me nuts when I read otherwise written, is tabs instead of spaces (especially when some in-app editors we use don't really have tab replacement functionalities, contributing considerably to pollution in the code base at prototyping stage).

诸如导入,什么导入,全局变量之类的东西的顺序.当大量导入的文件混合或出现乱序时,这确实使我大吃一惊.

Order of things such as imports, and what imports, globals etc. It really throws me off on files that have large amounts of imports when those are mixed up or out of order.

语句中的空格,尤其是当人们使用制表符并尝试使赋值操作在var名称中具有不同长度的行之间对齐时(而且似乎没有办法说服执行此操作的人使用看起来很出色的代码段)不整洁;)).

Whitespaces in statements, especially when people use tabs AND try to align assignment ops across lines with different length in var names (and there seems to be no way to persuade those who do it that an excel looking piece of code is NOT neater ;) ).

控制块内的间距,尤其是当我看到同一流程控制块内明显存在随机间距,然后在对象内使用相似数量的方法间距时.我不得不开始编辑那些东西,甚至无法阅读该死的东西.

And spacing within a control block, particularly when I see apparently random spacing within the same flow control block, and then similar amounts of spacing used within the object for methods. I'm compelled to edit those before I can even start reading the damn thing.

因此,这些都是我的,也是我违反" PEP背后的原因(有些人共有,有些人对此不以为然).我很想知道其他Pythonista使用者在这些方面做什么和不做什么.

So, those are mine, and the reasoning behind my "violations" of the PEP (some shared, some frowned upon by colleagues). I'd be very curious to read what other Pythonistas do and don't do in those regards.

推荐答案

每行79个字符"部分是胡说八道.他们自己的示例显示了执行此操作时代码变得不可读的方式:

The "79 characters per line" part is nonsense. Their very own example shows how unreadable code becomes when doing this:

class Rectangle(Blob):

    def __init__(self, width, height,
                 color='black', emphasis=None, highlight=0):
        if width == 0 and height == 0 and \
           color == 'red' and emphasis == 'strong' or \
           highlight > 100:
            raise ValueError("sorry, you lose")
        if width == 0 and height == 0 and (color == 'red' or
                                           emphasis is None):
            raise ValueError("I don't think so -- values are %s, %s" %
                             (width, height))
        Blob.__init__(self, width, height,
                      color, emphasis, highlight)

这就像尝试阅读
这样写的新闻文章

.

80列终端已经十多年没有成为一个严肃的开发环境了.当我确实需要在严重的80x25环境下进行编辑时,给编辑器换行带来的不便是个小麻烦;为了避免这种情况,我不会在正常开发期间破坏我的代码.

80-column terminals havn't been a serious development environment for over a decade. When I do need to edit from a crippled 80x25 environment in a pinch, editor wrapping is a minor inconvenience; I'm not going to maim my code during normal development just to avoid that.

120列换行对于现代开发是完全明智的,而我对140没问题.该指南已过时,遵循它会导致难看的,难以阅读的代码.

120 column wrapping is perfectly sensible for modern development, and I have no problem with 140. This guideline is obsolete and following it results in ugly, hard-to-read code.

这篇关于您会忽略哪些PEP 8准则,并坚持遵循哪些准则?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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