继续来码~python对象简介~

本文探讨了Python中对象的身份比对与类型检查的方法,包括使用is与is not操作符判断对象是否相同,通过type()和isinstance()函数确定对象类型,并介绍了可变与不可变类型的区别。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天北京重度污染~大家记得出门戴口罩

代码如下:

a = "abc"
b = "abc"
c = 1.23456
d = 1.23456
e = 100000000
f = 100000000
print id(a)
print id(b)
print id(c)
print id(d)
print id(e)
print id(f)
print a is b
print c is d
print e is f
a = "xyz"
print a is b
print a is not b</span>

输出结果:

4355332336
4355332336
140712956517568
140712956517568
140712956533840
140712956533840
True
True
True
False
True</span>

注:python提供了is和is not 操作符来判断两个变量是否指向同一个对象。


布尔逻辑操作符,and、or、not,优先级最高的为not,其次为,and和or。


标准类型的一些内建函数~

首先是type(),它接收一个对象作为参数,并返回它的类型。它的返回值是一个类型对象。

class Rabbit:pass

rabbit = Rabbit()

class Milk(object):pass

milk = Milk()

print type(Rabbit)
print type(rabbit)

print type(Milk)
print type(milk)</span>

输出结果:
<span style="font-size:14px;"><type 'classobj'>
<type 'instance'>
<type 'type'>
<class '__main__.Milk'></span>

接下来是cmp(a,b),用于比较两个对象,如果a<b则返回一个负整形,反之a>b则返回一个正整形,如果两者相等,则返回0。

内建函数str(),repr()或者反引号操作符,可以方便地以字符串的方式获取对象的内容。

print str(4.53-2j)
print str(2e10)
print str([0,5,10,15])
print repr(4.53-2j)
print repr(2e10)
print repr([0,5,10,15])
</span>
输出结果:
(4.53-2j)
20000000000.0
[0, 5, 10, 15]
(4.53-2j)
20000000000.0
[0, 5, 10, 15]</span>
比较一下两个函数:

repr返回的是一个对象的“官方”字符串表示,可以通过求值运算eval()重新得到该对象,但是str函数则不同,它致力于生成一个对象的可读性好的字符串表示,返回结果通常不可以用eval求值,但是很适合用print语句输出。另外,需要注意的是,并不是所有repr返回的字符串都可以用eval函数得到原来的对象。

检查数字对象类型,使用isinstance()函数:

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(69)
displayNumType(9999999999999L)
displayNumType(98.6)
displayNumType(-5.2+1.9j)
displayNumType("xyz")</span>

输出结果:
69 is a number of type: int
9999999999999 is a number of type: long
98.6 is a number of type: float
(-5.2+1.9j) is a number of type: complex
xyz is not a number at all</span>

另一个方案:

def displayNumType(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'
    elif type(num) == type(0.0):
        print 'a float'
    else:
        print 'not a number at all'</span>

改进方案:

1、减少函数调用的次数,可以提高程序性能:

import types
def displayNumType(num):
    print num,"is",
    if type(num) == types.IntType:
        print 'an integer'
    elif type(num) == types.LongType:
        print 'a long'
    elif type(num) == types.ComplexType:
        print 'a complex'
    elif type(num) == types.FloatType:
        print 'a float'
    else:
        print 'not a number at all'</span>
2、我们并不需要浪费时间去比较它们的值,只需要比较对象本身即可:

if type(num) is types.IntType

用对象身份的比较代替对象值的比较。

3、减少查询次数:

改进代码:from types import IntType

  if type(num) is IntType...


下面,说一下可变类型和不可变类型:

可变类型包括:列表和字典;

不可变类型包括:数字字符串和元组。

所谓的不可变类型,就是说当给不可变类型的对象更新时,其实是创建了新的对象,取代了旧对象:

x = "12345"
print id(x)
x = "tiger"
print id(x)

x = 1
print id(x)
x = x+1
print id(x)</span>

输出结果:

4390622864
4390623536
140279827944600
140279827944576</span>

可见,id已经发生了变化~

今天就到这里啦~大家晚安,兔几要去蹦哒一会儿了~


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值