python basestring( )
2016-05-06 14:19
721 查看
在上一篇博客(python2 type()函数 isinstance()函数,网址:http://blog.csdn.net/sxingming/article/details/51318939)中,我们学习了isinstance()函数的使用,在此基础上,本文介绍python 2 中的basestring()函数的使用。
basestring是str和unicode的超类(父类),是抽象类(The basestring type cannot be instantiated),不能被调用和实例化,但可以用来判断一个对象是否为str或者unicode的实例,
isinstance(obj, basestring)等价于isinstance(obj, (str, unicode));
python2.3版本以后引入该函数,兼容python2.3以后python2各版本。但是python3舍弃了该函数,所以该函数不能在python3中使用。
>>> type('hello python')
<type 'str'>
>>> isinstance('hello python',str)
True
>>> isinstance('hello python',basestring)
True
>>> type(u'哈喽')
<type 'unicode'>
>>> isinstance(u'哈喽',unicode)
True
>>> isinstance(u'哈喽',basestring)
True
>>> type(u'hello python')
<type 'unicode'>
>>> isinstance(u'hello python',unicode)
True
>>> isinstance(u'hello python',basestring)
True
basestring是str和unicode的超类(父类),是抽象类(The basestring type cannot be instantiated),不能被调用和实例化,但可以用来判断一个对象是否为str或者unicode的实例,
isinstance(obj, basestring)等价于isinstance(obj, (str, unicode));
python2.3版本以后引入该函数,兼容python2.3以后python2各版本。但是python3舍弃了该函数,所以该函数不能在python3中使用。
>>> type('hello python')
<type 'str'>
>>> isinstance('hello python',str)
True
>>> isinstance('hello python',basestring)
True
>>> type(u'哈喽')
<type 'unicode'>
>>> isinstance(u'哈喽',unicode)
True
>>> isinstance(u'哈喽',basestring)
True
>>> type(u'hello python')
<type 'unicode'>
>>> isinstance(u'hello python',unicode)
True
>>> isinstance(u'hello python',basestring)
True
相关文章推荐
- Anaconda多环境多版本python配置指导
- Python中的正则表达式(re)
- 在线生成UUID、Python生成唯一UUID(Universally Unique IDentifier )
- Python中 os 与 sys 模块
- Python语言中 import 与 reload 的使用问题
- python-面向对象的基本概念
- python之decode、encode及codecs模块
- Python的UTC时间转换
- 用Python抓取指定页面
- uWSGI 配置指令介绍
- DayDayUP_Python自学记录[5]_dict和set学习
- Python 类与元类的深度挖掘 II【经验】
- python matplotlib 画图show()不显示图
- Python 各进制间的转换
- Python 类与元类的深度挖掘 I【经验】
- Python 迭代器工具包【推荐】
- pythonwin-win32gui 窗口查找和遍历
- python对文件的操作
- python3 字符编码问题原理简介
- run SVN commands using python