python3.x 中的 StringType 和 NoneType
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4232111/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
StringType and NoneType in python3.x
提问by
I have a codebase which uses StringType and NoneType(types module) in the python2.x codebase. On porting to Python3, tests failed as the types module in Python3.x does not have the above mentioned two types.
我有一个代码库,它在 python2.x 代码库中使用 StringType 和 NoneType(类型模块)。在移植到 Python3 时,测试失败,因为 Python3.x 中的 types 模块没有上述两种类型。
I solved the problem by replacing them with "str" and "None" respectively. I was wondering if there is another (right)way of doing this. What I'm doing currently definitely seems to work, but I'm doubtful.
我通过分别用“str”和“None”替换它们来解决这个问题。我想知道是否有另一种(正确的)方法来做到这一点。我目前正在做的事情似乎确实有效,但我对此表示怀疑。
Should I stick to the approach I have followed or is there something wrong in what I have done? If so, how do I correct it?
我应该坚持我遵循的方法还是我所做的有问题?如果是这样,我该如何纠正?
采纳答案by Alexander Solovyov
Checking None is usually done by calling obj is None, while checking for string usually is isinstance(obj, str). In Python 2.x to detect both string and unicode, you could use isinstance(obj, basestring).
检查 None 通常通过调用来完成obj is None,而检查字符串通常是isinstance(obj, str). 在 Python 2.x 中检测字符串和 unicode,您可以使用isinstance(obj, basestring).
If you use 2to3, it's enough, but if you need to have single piece of code working in both Py2 and Py3, you may end up with something like that:
如果您使用2to3,这就足够了,但是如果您需要在 Py2 和 Py3 中使用一段代码,您最终可能会得到这样的结果:
try:
return isinstance(obj, basestring)
except NameError:
return isinstance(obj, str)
回答by Chris Morgan
Where possible I would recommend that you avoid the values in typesif their values are obvious; so for something like binding a method to an object, use types.MethodType, but for types.StringTypesuse (str, unicode)or rather basestring.
在可能的情况下,types如果它们的值很明显,我会建议您避免使用这些值;因此,对于将方法绑定到对象之类的事情,请使用types.MethodType,但对于types.StringTypes使用(str, unicode)或更确切地说basestring。
For this situation, I would do this:
对于这种情况,我会这样做:
- Use
obj is Noneorobj is not Nonerather thanisinstance(obj, NoneType)ornot isinstance(obj, NoneType). - Use
isinstance(obj, basestring)rather thanisinstance(obj, StringTypes) - Use
isinstance(obj, str)rather thanisinstance(obj, StringType)
- 使用
obj is Noneorobj is not None而不是isinstance(obj, NoneType)ornot isinstance(obj, NoneType)。 - 使用
isinstance(obj, basestring)而不是isinstance(obj, StringTypes) - 使用
isinstance(obj, str)而不是isinstance(obj, StringType)
Then, when you're needing to distribute for Python 3, use 2to3. Then your basestringwill become strand the rest will continue to work as it did before.
然后,当您需要为 Python 3 分发时,请使用2to3. 然后你的basestring意志变成str了,其余的将继续像以前一样工作。
(Also, bear in mind this, in particular the difference between StringTypeand StringTypes:
(另外,请记住这一点,特别是StringType和之间的区别StringTypes:
types.UnicodeType == unicode
types.StringType == str
types.StringTypes == (str, unicode)
)
)

