4.Python对象

来源:互联网 发布:js制作简单的树形菜单 编辑:程序博客网 时间:2024/05/16 19:13

妈呀,(Python核心编程),作者牛逼


1、Python对象的三个特性

a、身份

每一个对象都有唯一的身份证,其实就是内存地址啦,哈哈, 传说中 函数 id(传入对象),可以查看该对象的内存地址

>>> object = 'hello world'>>> id(object)44256608


b、类型

现在思考对象是什么类型?我们看到的类型是 str

>>> type(object)<type 'str'>>>> 

c、值

这个特性以后继续深入吧


2、对象属性

…………对象里面有什么变量可以搞…………

最常用的属性是方法和函数(函数与方法不是一回事吗?哥哥)

数据属性?: 类、类实例对象、模块、复数和文件…………表示不懂精神


3、基本数据类型有

int、bool、long、float、列表、元组、字典……


4、所有实例对象的类型的类型是type

不想了


5、None,返回空的对象, cao,不就是 null吗 就是0


6、下列实例对象的值是False

a、None

b、False

c、所有值为0 的数 (0就是False)

0、0.0、0L、0.0 + 0.0j、“”、[]、()、{}空列表、空元组、空字典,也会返回None


7、对象值的比较(比较对象本身,其实就对比内存地址),is 与 is not 是Python的关键字

>>> a = 5>>> b = a>>> a is bTrue


>>> a is not bFalse
比较a与b是否指向同一个对象


8、and、or、 not

与、或、非


9、type()再介绍

type(object):type函数,接受任意的object实例对象

返回值:返回它的类型

>>> type ('str')<type 'str'>
返回值是‘str’, 即 ‘str’的类型是str


10、cmp()介绍,对比两个基本数据类型的大小

>>> a, b = 1, 3>>> cmp(a, b)-1>>> cmp(b, a)1>>> 
如果 a < b 返回-1, 如果 b > a ,就会返回 1,这个函数有点意思, 如果两个对象相等,就会返回 0


11、str(),函数,就是接受任意类型的对象,返回一个String实例对象

>>> str(1)'1'>>> str(2 - 1)'1'>>> str ([1, 2, 3, 4])'[1, 2, 3, 4]'>>> str((1, 2, 3, 4))'(1, 2, 3, 4)'>>> 

12、pow()与 ** 均是作乘方运算

>>> 2 ** 38>>> pow(2, 3)8

13、Python不支持方法或者函数的重载

这点我还真没想到哈


14、isinstance(),这个函数,我看下用于判断一个实例对象是否属于其中的一个类型,有点意思

def displayNumType(num): #定义一个函数displayNumType,接受任意的对象参数 num    print num, 'is', #打印num is    if isinstance(num, (int, long, float, complex)): #isinstance(num, 属于的类型),如果num属于 元组中的类型(),返回 True        print 'a number of type:', type(num).__name__ #打印 a number of type:  type(num)得到类型.__name__ 这个字段是类型str名字    else:        print 'not a number at all!!!' #否则打印 not a number at all!!!displayNumType(-69) #调用函数displayNumType, 传入 -69 displayNumType('a')

注意点: print 会默认加上换行,但是如果 加上 逗号, 那就不会换行啦,而是代表空格字符

输出

>>> -69 is a number of type: inta is not a number at all!!!>>> 


15、没有的类型

char、byte,这两个类型,python中是没有

你可以用一个字符的字符串代表char,


指针(内存地址),python帮你搞定一切,因此你不能访问指针,其实在Python中一切都是指针














0 0
原创粉丝点击