小编Mat*_*ttS的帖子

在python中,为什么0xbin()返回False?

输入命令0xbin()返回False:

>>> 0xbin()
False
Run Code Online (Sandbox Code Playgroud)

为什么会这样?这种语法应该没有任何意义.函数不能以0开头,十六进制中没有"i"和"n",bin函数必须有一些参数.

python

191
推荐指数
4
解决办法
7842
查看次数

在python中,是否有某种映射返回类型的"假值"?

我正在寻找f()某种类似于此的映射函数:

f(str) = ''
f(complex) = 0j
f(list) = []
Run Code Online (Sandbox Code Playgroud)

这意味着它返回一个类型的对象,该对象在转换为时被计算Falsebool.

这样的功能存在吗?

python types boolean

42
推荐指数
4
解决办法
3561
查看次数

模式的正则表达式以ABC开头,然后是B和/或C,以CBA结束

假设我有一个只能包含A,B和C的字符串.

我有一个我要提取的某种模式的子串:它们从ABC开始,继续使用B和C的组合,并以CBA结束.

天真的解决方案是使用ABC[BC]*CBA.

但是,这不会涵盖ABCBA字符串.除了|用于寻找两个可能的RE 之外,还有"pythonic"方法来解决这个问题吗?

python regex

2
推荐指数
1
解决办法
137
查看次数

In python, how to refer to NoneType?

Let's say I have on object x. I want to check if x is of a type I support (let's say it's lists, sets, strings and None. And without types that inherit from them), so I figured I'll just check

type(x) in (list, set, str, NoneType)
Run Code Online (Sandbox Code Playgroud)

但是, NoneType 不能被引用。有没有干净的解决方案,还是我必须做这样的事情?

x is None or type(x) in (list, set, str)
Run Code Online (Sandbox Code Playgroud)

有标准的访问方式<type 'NoneType'>吗?

谢谢。

python

0
推荐指数
1
解决办法
1224
查看次数

标签 统计

python ×4

boolean ×1

regex ×1

types ×1