pep

how to avoid python numeric literals beginning with “0” being treated as octal?

大兔子大兔子 提交于 2019-12-19 13:48:13
问题 I am trying to write a small Python 2.x API to support fetching a job by jobNumber , where jobNumber is provided as an integer. Sometimes the users provide a jobNumber as an integer literal beginning with 0, e.g. 037537 . (This is because they have been coddled by R, a language that sanely considers 037537==37537 .) Python, however, considers integer literals starting with "0" to be OCTAL, thus 037537!=37537 , instead 037537==16223 . This strikes me as a blatant affront to the principle of

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

亡梦爱人 提交于 2019-12-18 19:05:27
问题 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? 回答1: 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

标准的Python文档字符串格式是什么? [关闭]

痴心易碎 提交于 2019-12-18 17:15:11
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 已关闭 。 这个问题是基于意见的。 它当前不接受答案。 了解更多 。 想改善这个问题吗? 更新问题,以便通过 编辑此帖子 以事实和引用的形式回答。 去年 关闭。 我已经看到了几种用Python编写文档字符串的不同样式,是否有正式的或“同意的”样式? #1楼 是Python; 一切顺利 。 考虑如何 发布您的文档 。 除了您的源代码读者以外,文档字符串是不可见的。 人们真的很喜欢浏览和搜索网络上的文档。 为此,请使用文档工具 Sphinx 。 这是记录Python项目的实际标准。 该产品非常漂亮-请看 https://python-guide.readthedocs.org/en/latest/ 。 “ 阅读文档 ”网站将免费托管您的文档。 #2楼 显然没有人提到它:您还可以使用 Numpy Docstring Standard 。 它在科学界被广泛使用。 numpy 格式 的 说明以及 示例 您有一个狮身人面像扩展程序来渲染它: numpydoc 还有一个呈现的文档字符串看起来如何美丽的示例: http : //docs.scipy.org/doc/numpy/reference/generation/numpy.mean.html 用于解析Google样式文档字符串的Napolean狮身人面像扩展名(在

Triple-double quote v.s. Double quote

我的梦境 提交于 2019-12-18 12:08:43
问题 What is the preferred way to write Python doc string? """ or " In the book Dive Into Python, the author provides the following example: def buildConnectionString(params): """Build a connection string from a dictionary of parameters. Returns string.""" In another chapter, the author provides another example: def stripnulls(data): "strip whitespace and nulls" return data.replace("\00", "").strip() Both syntax work. The only difference to me is that """ allows us to write multi-line doc. Is

有人可以用Python解释__all__吗?

一曲冷凌霜 提交于 2019-12-16 20:09:16
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 我越来越多地使用Python,并且不断看到在不同的 __init__.py 文件中设置了变量 __all__ 。 有人可以解释这是什么吗? #1楼 我只是添加这是为了精确: 所有其他答案均涉及 模块 。 最初的问题在 __init__.py 文件中明确提到 __all__ ,所以这是关于python 包的 。 通常, __all__ 仅在使用 import 语句的 from xxx import * 变体时才起作用。 这适用于软件包以及模块。 模块的行为在其他答案中进行了说明。 包的确切行为 在此处 详细描述。 简而言之,包级别的 __all__ 与 模块 具有几乎相同的功能,除了它处理 包中的模块 (与 在模块中 指定 名称 相反)。 因此 __all__ 指定当我们 from package import * 使用时应加载并导入到当前名称空间的所有模块。 最大的区别是,当您 省略 软件包的 __init__.py 的 __all__ 声明时 from package import * 的语句将根本不会导入任何内容(文档中有例外说明,请参见上面的链接)。 另一方面,如果在模块中省略 __all__ ,则“已加星标的导入”将导入模块中定义的所有名称(不以下划线开头)。 #2楼 它还更改了pydoc将显示的内容:

如何在Python中表示“枚举”?

早过忘川 提交于 2019-12-11 16:45:25
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 我主要是C#开发人员,但目前正在使用Python开发项目。 我怎样才能代表Python中的Enum? #1楼 def M_add_class_attribs(attribs): def foo(name, bases, dict_): for v, k in attribs: dict_[k] = v return type(name, bases, dict_) return foo def enum(*names): class Foo(object): __metaclass__ = M_add_class_attribs(enumerate(names)) def __setattr__(self, name, value): # this makes it read-only raise NotImplementedError return Foo() 像这样使用它: Animal = enum('DOG', 'CAT') Animal.DOG # returns 0 Animal.CAT # returns 1 Animal.DOG = 2 # raises NotImplementedError 如果您只需要唯一的符号并且不关心值,请替换此行: __metaclass__ = M_add

在现代Python中声明自定义异常的正确方法?

心已入冬 提交于 2019-12-11 12:02:33
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 在现代Python中声明自定义异常类的正确方法是什么? 我的主要目标是遵循其他异常类具有的任何标准,以便(例如)我捕获到异常中的任何工具都会打印出我包含在异常中的任何多余字符串。 “现代Python”是指可以在Python 2.5中运行但对于Python 2.6和Python 3. *是“正确”的方式。 所谓“自定义”,是指一个Exception对象,该对象可以包含有关错误原因的其他数据:字符串,也许还包括与该异常相关的其他任意对象。 我在Python 2.6.2中被以下弃用警告绊倒了: >>> class MyError(Exception): ... def __init__(self, message): ... self.message = message ... >>> MyError("foo") _sandbox.py:3: DeprecationWarning: BaseException.message has been deprecated as of Python 2.6 BaseException 对于名为 message 属性有特殊含义似乎很疯狂。 我从 PEP-352 收集到,该属性确实在2.5中有特殊含义,因此他们想弃用该属性,所以我猜想现在禁止使用该名称(以及一个人)。 啊。

对象名称前的单下划线和双下划线是什么意思?

牧云@^-^@ 提交于 2019-12-10 20:39:01
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 有人可以解释一下在Python中对象名称前加下划线的确切含义吗? 另外,请说明单首和双首下划线之间的区别。 此外,无论所讨论的对象是变量,函数,方法等,该含义是否都保持不变? #1楼 ._variable 是 ._variable 的,仅用于约定 .__variable 通常被错误地认为是超私有的,而其实际含义只是为了 防止意外访问而使用 namemangle [1] .__variable__ 通常保留给内置方法或变量 如果您非常需要,仍可以访问 .__mangled 变量。 双下划线仅将变量命名或将变量重命名为 instance._className__mangled 例: class Test(object): def __init__(self): self.__a = 'a' self._b = 'b' >>> t = Test() >>> t._b 'b' t._b是可访问的,因为它仅被约定隐藏 >>> t.__a Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'Test' object has no attribute '__a' 找不到t .__ a

Numpy type hints in Python (PEP 484)

这一生的挚爱 提交于 2019-12-10 12:58:54
问题 I would like to add type hints to a method that takes a numpy array as an input, and returns a string. This numpy array contains floats so I tried: import numpy as np def foo(array: np.ndarray[np.float64]) -> str: But it will not work due to a TypeError: 'type' object is not subscriptable . I found this but could not follow the discussions! 回答1: Check out nptyping. It offers type hints for numpy arrays. In your case, you would end up with: import numpy as np from nptyping import Array def foo

x002-语言元素

有些话、适合烂在心里 提交于 2019-12-10 06:18:28
变量命令规则 硬性规则: 变量名由字母(广义的Unicode字符,不包括特殊字符)、数字和下划线构成,数字不能开头。 大小写敏感(大写的 a 和小写的 A 是两个不同的变量)。 不要跟关键字(有特殊含义的单词,后面会讲到)和系统保留字(如函数、模块等的名字)冲突。 PEP 8要求: 用小写字母拼写,多个单词用下划线连接。 受保护的实例属性用单个下划线开头。 私有的实例属性用两个下划线开头。 a = 100 b = 12.345 c = 1 + 5j d = 'hello, world' e = True print(type(a)) # <class 'int'> print(type(b)) # <class 'float'> print(type(c)) # <class 'complex'> print(type(d)) # <class 'str'> print(type(e)) # <class 'bool'> 内置函数进行转换 int() :将一个数值或字符串转换成整数,可以指定进制。 float() :将一个字符串转换成浮点数。 str() :将指定的对象转换成字符串形式,可以指定编码。 chr() :将整数转换成该编码对应的字符串(一个字符)。 ord() :将字符串(一个字符)转换成对应的编码(整数)。 示例 root@jenkins:/data/python#