pep

如何在python字符串中打印文字大括号字符并在其上使用.format?

China☆狼群 提交于 2019-12-29 12:08:19
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> x = " \{ Hello \} {0} " print x.format(42) 给我: Key Error: Hello\\\\ 我要打印输出: {Hello} 42 #1楼 尽管没有更好的效果,但仅供参考,您也可以这样做: >>> x = '{}Hello{} {}' >>> print x.format('{','}',42) {Hello} 42 例如,当有人要打印 {argument} 时,它可能很有用。 它可能比 '{{{}}}'.format('argument') 更具可读性 请注意,在Python 2.7之后,您省略了参数位置(例如,用 {} 代替 {0} ) #2楼 OP写了这个评论: 我试图出于某种目的格式化小型JSON,例如: '{"all": false, "selected": "{}"}'.format(data) 以获取类似 {"all": false, "selected": "1,2"} 在处理JSON时经常会出现“转义括号”问题。 我建议这样做: import json data = "1,2" mydict = {"all": "false", "selected": data} json.dumps(mydict) 它比替代方案更清洁,替代方案是: '{{"all"

【译】PEP-3129 类装饰器

我是研究僧i 提交于 2019-12-27 20:25:31
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> PEP原文 : https://www.python.org/dev/peps/pep-3129 PEP标题: Class Decorators PEP作者: Collin Winter 创建日期: 2007-05-01 合入版本: 3.0 译者 : 豌豆花下猫 ( Python猫 公众号作者) PEP翻译计划 : https://github.com/chinesehuazhou/peps-cn 摘要 本 PEP 提议推出类装饰器,它是对 PEP-318 引入的函数与方法(function and method)装饰器的扩展。 原理阐述 当初讨论函数装饰器是否该在 Python 2.4 中引入时,由于有元类,所以类装饰器被视为晦涩且不必要的[1]。但是,在使用 Python 2.4.x 系列发行版本的几年后,对函数装饰器及其使用的日益熟悉之后,BDFL 和社区重新评估了类装饰器,并建议将其包含在 Python 3.0 中[2]。 这个改变的目的是使某些构造更易于表达,并且减少对 CPython 解释器的实现细节的依赖。尽管可以使用元类来实现类似装饰器(decorator-like)功能的类,但结果通常令人不快,实现起来也很脆弱[3]。另外,元类是要继承的,而类装饰器则不是

Better to 'try' something and catch the exception or test if it's possible first to avoid an exception?

三世轮回 提交于 2019-12-27 09:54:12
问题 Should I test if something is valid or just try to do it and catch the exception? Is there any solid documentation saying that one way is preferred? Is one way more pythonic ? For example, should I: if len(my_list) >= 4: x = my_list[3] else: x = 'NO_ABC' Or: try: x = my_list[3] except IndexError: x = 'NO_ABC' Some thoughts... PEP 20 says: Errors should never pass silently. Unless explicitly silenced. Should using a try instead of an if be interpreted as an error passing silently? And if so,

Better to 'try' something and catch the exception or test if it's possible first to avoid an exception?

夙愿已清 提交于 2019-12-27 09:53:32
问题 Should I test if something is valid or just try to do it and catch the exception? Is there any solid documentation saying that one way is preferred? Is one way more pythonic ? For example, should I: if len(my_list) >= 4: x = my_list[3] else: x = 'NO_ABC' Or: try: x = my_list[3] except IndexError: x = 'NO_ABC' Some thoughts... PEP 20 says: Errors should never pass silently. Unless explicitly silenced. Should using a try instead of an if be interpreted as an error passing silently? And if so,

Python`如果x不是None`或`if not x is None`?

两盒软妹~` 提交于 2019-12-26 21:19:33
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 我一直认为 if not x is None 版本更清楚,但谷歌的 风格指南 和 PEP-8 都使用, if x is not None 。 是否存在任何轻微的性能差异(我假设没有),是否存在任何一个真正不合适的情况(使另一个成为我公约的明显赢家)?* *我指的是任何单身人士,而不仅仅是 None 。 ...比较像无的单身人士。 使用是否。 #1楼 谷歌和 Python 的风格指南都是最佳实践: if x is not None: # Do something about x 使用 not x 会导致不需要的结果。 见下文: >>> x = 1 >>> not x False >>> x = [1] >>> not x False >>> x = 0 >>> not x True >>> x = [0] # You don't want to fall in this one. >>> not x False 您可能有兴趣在Python中查看哪些文字被评估为 True 或 False : 真值测试 编辑以下评论: 我刚做了一些测试。 not x is None 不首先否定 x 然后与 None 进行比较。 事实上,它似乎 is 运营商采用了这种方式,当一个更高的优先级: >>> x [0] >>> not x

Where are detailed the rules, concepts and usages behind each pylint's warnings?

北城余情 提交于 2019-12-25 18:19:51
问题 I am still discovering Pylint and I understand why many Pythonists deactivate some (or many) warnings to lower Pylint 's voice, but for the moment, as a Python newbie I want to use pylint to improve my Pythonic comprehension and my code quality , in order to help me to: learning more from a statement/instruction deepen some concepts evaluate the benefits / drawback of a refactoring etc. So is there a place where all the warning are discussed, justified, explained or they simply came from the

Python中变量和函数名称的命名约定是什么?

人走茶凉 提交于 2019-12-23 22:50:05
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 来自C#背景,变量和方法名称的命名约定通常是camelCase或PascalCase: // C# example string thisIsMyVariable = "a" public void ThisIsMyMethod() 在Python中,我已经看到了上面的内容,但我也看到了使用下划线: # python example this_is_my_variable = 'a' def this_is_my_function(): Python有更优选的,明确的编码风格吗? #1楼 请参阅Python PEP 8 。 函数名称应为小写,并根据需要用下划线分隔,以提高可读性。 只有在已经成为流行风格的情境中才允许使用mixedCase 变量... 使用函数命名规则:小写,必要时用下划线分隔,以提高可读性。 就个人而言,我与此不同,因为我也喜欢 mixedCase 在 lower_case 为我自己的项目。 #2楼 编码风格通常是组织内部策略/约定标准的一部分,但我认为通常,all_lower_case_underscore_separator样式(也称为snake_case)在python中最常见。 #3楼 正如其他答案所示,有 PEP 8 ,但PEP 8只是标准库的样式指南,并且它仅作为福音。 PEP

Does Python evaluate type hinting of a forward reference?

眉间皱痕 提交于 2019-12-23 03:28:12
问题 I was looking at the PEP 484 section on Forward References and noticed the statement: ...that definition may be expressed as a string literal, to be resolved later. And that got me wondering, when is "later" and by what? The interpreter doesn't try to resolve it as a literal later, so what does? Is it just if a third party tool is written to do that? Small example to demonstrate the interpreter result: class A: def test(self, a: 'A') -> None: pass class B: def test(self, a: A) -> None: pass >

Which implementation of OrderedDict should be used in python2.6?

二次信任 提交于 2019-12-22 05:23:35
问题 As some of you may know in python2.7/3.2 we'll get OrderedDict with PEP372 however one of the reason the PEP existed was because everyone did their own implementation and they were all sightly incompatible. So which one of the 8 current implementations in the PEP is backwards compatible with the 2.7 odict from python 2.7 in a way we can start using that now and depend on 2.7 in a couple of months? 回答1: This package (for Python 2.4 or better) claims to be "A drop-in substitute for Py2.7's new

Tool for automatically check docstring style according to PEP257 [closed]

强颜欢笑 提交于 2019-12-20 17:27:19
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 3 years ago . Tools like pep8 can check source code style, but they don't check if docstrings are fromatted according to pep257, pep287. Are there such tools? Update I decided to implement such a static analysis tool on my own, see: https://github.com/GreenSteam/pep257 Right now, most of pep257 is covered. Design was heavily