Python对象
2016-06-05 22:08
381 查看
python对象
所有的python对象都有三个特性: 身份(id) 类型(type) 值(=) 类型对象和type类型对象
>>> type(123) <type 'int'> >>> type(type(123)) <type 'type'>
python的内部类型 代码对象 代码对象是经过编译的python代码片段,是可执行对象。 切片对象 语法:aequence[起始索引,结束索引,步进值]
>>> victor = "fengxiao" >>> victor[::-1] 'oaixgnef' >>> victor[::-2] 'oige' >>> success = [123,'abc', 'htc', 'apple', 'nokia'] >>> success[::-1] ['nokia', 'apple', 'htc', 'abc', 123]
XRange 对象计 xrange内建函数会生成XRange对象,xrange()是内建函数range()的兄弟版本,用于节省内存使用或range()无法完成的超大数据场合
对象身份比较:
>>> a = 123 >>> b = 123 >>> a is b True >>> id(a) == id(b) True
下面就有点奇妙了
>>> c = 1.0 >>> d = 2.0 >>> id(c) 48271032 >>> id(d) 48270984 >>> c is d False
为啥c和d的id会不一样呢? 这是因为python仅会缓存整形对象,python的缓存的整数范围是多少呢?我们编个程序看一下:
var1 = 0 var2 = 0 while True: if var1 is not var2: #id(var1) != id(var2) #up #print 'bingo!:%d' % (var1-1) #break #down print 'bingo!:%d' % (var1+1) break #up var1 += 1 var2 += 1 #bingo!:256 #down # var1 -= 1 # var2 -= 1 #bingo!:-5
也就是说在python2.7.8中python的缓存范围是[-5,256] python标准类型的内建函数 type()-返回对象的类型,且不局限于标准类型。 cmp()-比较两个对象的ASCII码值的大小,类似于C语言的strcmp()函数;但是也有不同C语言中比较 字符串的大小,而python比较对象的,而且只返回1,-1,0而C语言返回的是随意的负数、正数、0。
>>> cmp(a, b) -1 >>> cmp(b, a) 1 >>> b = -4 >>> a = -4 >>> cmp(a, b) 0 >>> a, b = 'abc', 'cnd' >>> cmp(a, b) -1 >>> a, b = 'abc', 'xyz' >>> cmp(a, b) -1 str()和 repr()(以及``) >>> str(5.32-3j) '(5.32-3j)' >>> str(1) '1' >>> str(2e10) '20000000000.0' >>> str([1, 3, 4, 6]) '[1, 3, 4, 6]' >>> repr([2, 3, 5, 0]) '[2, 3, 5, 0]' >>> `[1, 2, 3, 5]` '[1, 2, 3, 5]'
str()致力于生成一个对象可读性好的字符串表示。 repr()和``特性和功能性方面非常相似,repr()返回的也是一个对象的“官方字符串表示,大多数情况下可以通过求值运算。 简而言之,一句话"repr输出对python比较友好,str输出对用户比较友好" isinstance()用来判断第一个参数是否是第二个参数的标准类型、实例对象、元祖。
>>> help(isinstance) ''' Help on built-in function isinstance in module __builtin__: isinstance(...) isinstance(object, class-or-type-or-tuple) -> bool Return whether an object is an instance of a class or of a subclass thereof. With a type as second argument, return whether that is the object's type. The form using a tuple, isinstance(x, (A, B, ...)), is a shortcut for isinstance(x, A) or isinstance(x, B) or ... (etc.). '''
python不支持方法和函数的重载。
>>> class C1: pass >>> class C2: pass >>> c1 = C1() >>> c2 = C2() >>> isinstance(c1, C1) True >>> isinstance(c2,C2) True >>> isinstance(c1,C2) False >>> isinstance(2, int) True >>> isinstance(4, str) False >>> isinstance('2', str) True >>> a = 1 >>> isinstance(a, (str,list,tuple)) False >>> isinstance(a, (str,list,tuple,int)) File "<pyshell#43>", line 1 isinstance(a, (str,list,tuple,int)) ^ IndentationError: unexpected indent >>> isinstance(a, (str,list,tuple,int)) True
#判断a是否符合后面元祖的类型。
相关文章推荐
- Python动态类型的学习---引用的理解
- Python3写爬虫(四)多线程实现数据爬取
- 垃圾邮件过滤器 python简单实现
- 下载并遍历 names.txt 文件,输出长度最长的回文人名。
- install and upgrade scrapy
- Scrapy的架构介绍
- Centos6 编译安装Python
- 使用Python生成Excel格式的图片
- 让Python文件也可以当bat文件运行
- [Python]推算数独
- Python中zip()函数用法举例
- Python中map()函数浅析
- Python将excel导入到mysql中
- Python在CAM软件Genesis2000中的应用
- 使用Shiboken为C++和Qt库创建Python绑定
- FREEBASIC 编译可被python调用的dll函数示例
- Python 七步捉虫法