python对象

上一篇 / 下一篇  2013-10-15 20:07:56 / 个人分类:python

#1、python对象
#python对象有三个特性:
    #1、身份:每个都拥有一个唯一的身份标识,可以使用内建函数id()来得到,可认为是对对象的内存地址
    #2、类型,对象的类型决定了带对象可以保持什么类型的值,进行什么样的操作,遵循什么样的原则,使用type()函数查看
    #3、值:对象标识的数据项
#2、标准类型:Integer 整型  Blooean:布尔型  Long Integer:长整型 Floating point real number:浮点型
        #Complex number :复数型   String :字符串  List:列表  Tupe:元组  Dictionary:字典

#3、其他内建类型:Null(None),文件,集合,函数/方法 模块 类
print type(43)  #输出:<type 'int'>,类就是类型,实例就是类型的对象.type返回的是类型对象
print type(type(43))  #输出<tyoe 'type'>:所有类型对象的类型都是type
#Null对象或者NoneType,它只有一个值,就是None.None的布尔值总是False
#空对象、值为0的任何数字、Null对象None的布尔值都为False【0,0.0,0.0+0j,'',[],(),{}】

#4、内部类型:
  #a.代码对象:是编译过的python源代码,可执行对象。通过调用内建函数compile()可以得到代码对象,可以使用exec命令和eval()内建函数来执行
  #b.帧对象:python的执行栈帧,帧对象包含python解释器在运行时所需要知道的所有信息,他的属性包含’指向上一帧的连接、被执行的代码对象、本地及全局名称空间字典及当前指令等‘
  #c.跟踪记录对象:代码出错时,python会引发一个异常,如果未捕获,解释器会退出脚本并显示诊断信息
  #d.切片对象:切片对象有内建函数slice()生成。如下例子:
foostr = 'abcdeddsgd'
print foostr[::-1] #步进切片:sequence【起始索引:结束索引:步进值】
print foostr[::-2]
print foostr[::1]
print foostr[:]  #不写数字表示:起始不写从0开始,结尾不谢表示为结束值得索引
print foostr[:3] #0 1 2 的索引对应的值输出
print foostr[0:-1] #注意结束索引对应的值不会输出
#省略对象:用于扩展切片语法中,起记号作用,名字:Ellipsis,值始终为True
#XRange对象:类似于range()的兄弟版本,用于节省使用或range()无法完成的超大数据集合

#5、标准类型操作符
#对象值的比较:判断同类型对象是否相等,返回Boolean类型:> ,<, >=, <=,==,!= <> 
print 2==2
print 2.43<22
print 'abd' >'xyz'
print [3,'abc'] == ['abc',3]
print 3<4<6  #等同于 3< 4 and 4 <6
print 3<4 == 4  #等同于 (3< 4) and (4 == 4)
print '-----------------'
#对象身份的比较
#foo1 foo2 指向相同的对象
foo1 = foo2 = 33  #True
print foo1 == foo2
foo1 = 3
print foo2 == foo1       #True
#foo1 foo2 指向不同的对象
foo1 = 3.1
foo2 = 3.1
print foo1 == foo2 
#python可以用a is b ,a is not b 来判断两个变量是指向同一个对象
print foo1 is foo2 #等价于foo1 == foo2
print foo1 is not foo2 
print id(foo1)
print id(foo2)#foo1 和foo2指向同一块内存
#布尔类型
#布尔类型:and :逻辑与、 or:逻辑或、not:逻辑非为关键字
(x,y) = (3.23,-33)
print x < 5.0
print not (x < 5.0) #False
print (x < 5.0) or (y > 10.0)
print (x < 5.0) and (y > 10.0)
print not (x is y)  #True


#6、标准类型内建函数
#type():type(object),返回参数的类型,是一个类型对象type(4)--><type 'int'>  type('hello world') --><type 'string'>
#cmp(a ,b):比较两个对象,如果a >b 返回一个整数,a < b返回一个负数,a = b 返回0
a ,b = -4,12
print cmp(a,b)  #-1
a, b = 'abc','xyz'
print cmp(a,b)  #-23
#str(), repr() 和''(反引号操作符):以字符串的方式获取对象的内容、类型、数值属性等信息
print str(4.54-2j)
print str(1)
print str(2e10)
print repr([0,5,4,3])
print '[0,5,9,9]'
#type() 和isinstance()函数:
class Foo:pass
foo = Foo()
class Bar(Foo):pass
bar = Bar()
print type(foo)
print type(bar)
print type(Bar)
#检查类型的函数
def displayNumType(num):
    print num,'is',
    if isinstance(num,(int ,long,float,complex)):
        print 'a number of type:',type(num).__name__
    else:
        print 'not a number at all!'
displayNumType(-89)
displayNumType(99999999999999L)

#
def displayNumType2(num):
    print num,'is',
    if type(num) == type(0):
        print 'an integer'
    elif type(num) == type(0L):
        print 'a long'
    elif type(num) == type(0 + 0j):
        print 'a complex number'
    else:
        print 'not a number at all'
displayNumType(99999999999999L)
displayNumType(-89)
s = 'abc'
s2 = 'abc'
s3 = 'a'+'bc'
print id(s)
print id(s2)
print id(s3)   #地址块都是相同的
c = -2.3
d = -2.3
print id(c)
print id(d)     #地址块是相同的
e = ('aaa','vvv')
f = ('aaa','vvv')
print id(e[0])
print id(f[0])  #地址块是相同的
print id(e)
print id(f)     #地址块不同





TAG:

 

评分:0

我来说两句

日历

« 2024-03-24  
     12
3456789
10111213141516
17181920212223
24252627282930
31      

数据统计

  • 访问量: 15156
  • 日志数: 25
  • 建立时间: 2013-07-27
  • 更新时间: 2013-10-22

RSS订阅

Open Toolbar