V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
hush
V2EX  ›  Python

关于 str 的类型转换

  •  
  •   hush · 2015-01-29 14:03:27 +08:00 · 3201 次点击
    这是一个创建于 3372 天前的主题,其中的信息可能已经有所发展或是发生改变。
    问题:判断来两个变量dataA,dataB是否为str类型,一个是则输出'Ture'
    方法1:if dataA==type(dataA) or dataB==type(dataB):
    print ’True‘
    方法2:if dataA==str(dataA) or dataB==str(dataB):
    print ’True‘
    现在的问题:第二个方法中的str(dataA)的执行是否会有错误,即Python中强制转换是否可行?如果不行能否给我一个反例
    12 条回复    2015-01-29 18:44:57 +08:00
    ruoyu0088
        1
    ruoyu0088  
       2015-01-29 14:13:56 +08:00
    你的程序和你的问题对不上啊。
    raquelken
        2
    raquelken  
       2015-01-29 14:17:23 +08:00
    python里面判断类型的方法是 isinstance
    broono
        3
    broono  
       2015-01-29 14:24:13 +08:00
    不应该是这样吗:
    if isinstance(dataA,'str') and isinstance(dataB,'str'):
    print 'True'
    hush
        4
    hush  
    OP
       2015-01-29 14:28:10 +08:00
    @ruoyu0088 就是现在用的方法不是考虑不全面
    hush
        5
    hush  
    OP
       2015-01-29 14:30:10 +08:00
    上面的两个方法都能运行得到效果,但就第二种不全面
    hahastudio
        6
    hahastudio  
       2015-01-29 14:35:36 +08:00
    你的问题大概是你觉得找到了

    >>> s = "a"
    >>> isinstance(s, str)
    True
    >>> type(s) == str
    True
    >>> class bar(str):
    def __init__(self):
    pass

    >>> b = bar()
    >>> type(b) == str
    False
    >>> isinstance(b, str)
    True

    然后你要知道 x.__str__() <==> str(x) 就可以了
    str(x) 就相当于其他语言的 x.ToString()
    hush
        7
    hush  
    OP
       2015-01-29 14:46:16 +08:00
    @hahastudio 你的意思是 str(X)使用这个是不会有错的吗?
    buru
        8
    buru  
       2015-01-29 14:59:07 +08:00
    肯定不会有错误的 ,每个类型都有__str__方法
    @hush
    hush
        9
    hush  
    OP
       2015-01-29 15:12:39 +08:00 via Android
    @buru 嗯,好的谢谢了
    Valyrian
        10
    Valyrian  
       2015-01-29 15:24:08 +08:00
    方法一是错的,左边是str右边是type
    方法二也是错的,如果dataA类型的__eq__()方法允许和str相等的话

    建议isinstance(dataA, str)或者type(dataA) == type("")
    Valyrian
        11
    Valyrian  
       2015-01-29 15:28:40 +08:00
    补充一句,如果dataA是一个str的子类,isinstance()会true,type(dataA) == type("")会false
    所以你如果想要dataA是严格的str就用type(dataA) == type(""),如果不在乎是子类就用isinstance()
    zhicheng
        12
    zhicheng  
       2015-01-29 18:44:57 +08:00
    >>> isinstance('abc', str)
    True
    >>> isinstance(u'abc', str)
    False
    >>> isinstance('abc', unicode)
    False
    >>> isinstance(u'abc', unicode)
    True
    >>> isinstance('abc', basestring)
    True
    >>> isinstance(u'abc', basestring)
    True
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5629 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 06:40 · PVG 14:40 · LAX 23:40 · JFK 02:40
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.