输入命令0xbin()返回False:
>>> 0xbin()
False
Run Code Online (Sandbox Code Playgroud)
为什么会这样?这种语法应该没有任何意义.函数不能以0开头,十六进制中没有"i"和"n",bin函数必须有一些参数.
我正在寻找f()某种类似于此的映射函数:
f(str) = ''
f(complex) = 0j
f(list) = []
Run Code Online (Sandbox Code Playgroud)
这意味着它返回一个类型的对象,该对象在转换为时被计算False为bool.
这样的功能存在吗?
假设我有一个只能包含A,B和C的字符串.
我有一个我要提取的某种模式的子串:它们从ABC开始,继续使用B和C的组合,并以CBA结束.
天真的解决方案是使用ABC[BC]*CBA.
但是,这不会涵盖ABCBA字符串.除了|用于寻找两个可能的RE 之外,还有"pythonic"方法来解决这个问题吗?
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'>吗?
谢谢。