python中一切皆对象
python中一切皆对象
1,python中一切皆对象
首先,对象应该是某一个类型的实例,也就是模板刻出来的模型
其次,对象应该在内存中有一块区域用于存放它,应该有存放它的内存地址
最后,对象应该有具体的值,也就是它肚子里的东西
也就是说 一个对象应该具有 类型 内存地址 和值
python中一切皆对象包括( 类 具体对象 方法 函数 模块 io … )
1.1 类和方法也是对象
python中类和方法也是对象 也可以赋值给另一个变量 或者当做参数 返回值。
#-*-coding:utf-8-*-
#类和方法都是对象
class Student:
def __init__(self):
print("Student init print -----")
def func():
print("function print-----")
def add(f,c):
'''
类和函数当做参数传递
:param f:
:param c:
:return:
'''
li = []
li.append(f)
li.append(c)
return li
# 函数当做返回值
def re_func():
print("re_func--------")
return func
# print("类当做返回值")
def re_cla():
print("re_cla-----")
return Student
if __name__ == '__main__':
#赋值给另一个变量
print("赋值给另一个变量")
Studentcla = Student
xiaoming = Studentcla()
funcval = func
funcval()
reli = add(func,Student)
#类和函数当做参数传递
print("类和函数当做参数传递")
for item in reli:
item()
print("函数当做返回值")
f = re_func()
f()
print("类当做返回值")
c = re_cla()
c()
输出结果:
赋值给另一个变量
Student init print -----
function print-----
类和函数当做参数传递
function print-----
Student init print -----
函数当做返回值
re_func--------
function print-----
类当做返回值
re_cla-----
Student init print -----
1.2 对象的 类型 值 地址
既然Python中一切接对象 那么看看对象的 类型 值 地址
#-*-coding:utf-8-*-
import re
def print_detail(params):
if isinstance(params,list):
for item in params:
print("值:{} 类型:{} 地址:{}".format(item, type(item), id(item)))
else:
item = params
print("值:{} 类型:{} 地址:{}".format( item, type(item), id(item)))
#python中基本类型和集合
a = 1
b = "abc"
c = [1,2,3]
f = open("aaa.txt",mode="r")
print_detail([a,b,c,int,str,list,f,re,print_detail])
print("---------------------------")
#python中的类和对象
class Student:
def __init__(self,name):
self.name = name
class SmallStudent(Student):
pass
'''
zhangsan是Student的实例
Student类时type的实例
所有类的父类object都是type的实例
连type自己都是type的实例
而type的父类有是object
'''
zhangsan = Student("zhangsan")
print_detail([zhangsan,Student,object,type])
print("---------------------------")
print("Student的父类:",Student.__bases__)
print("object的父类:",object.__bases__)
print("type的父类:",type.__bases__)
输出结果:
值:1 类型:<class 'int'> 地址:10910400
值:abc 类型:<class 'str'> 地址:140168397282752
值:[1, 2, 3] 类型:<class 'list'> 地址:140168371728904
值:<class 'int'> 类型:<class 'type'> 地址:10283392
值:<class 'str'> 类型:<class 'type'> 地址:10294272
值:<class 'list'> 类型:<class 'type'> 地址:10259072
值:<_io.TextIOWrapper name='aaa.txt' mode='r' encoding='UTF-8'> 类型:<class '_io.TextIOWrapper'> 地址:140168372296296
值:<module 're' from '/usr/lib/python3.6/re.py'> 类型:<class 'module'> 地址:140168372212344
值:<function print_detail at 0x7f7b7f8b1ea0> 类型:<class 'function'> 地址:140168397528736
---------------------------
值:<__main__.Student object at 0x7f7b7e02e710> 类型:<class '__main__.Student'> 地址:140168371824400
值:<class '__main__.Student'> 类型:<class 'type'> 地址:40525336
值:<class 'object'> 类型:<class 'type'> 地址:10293856
值:<class 'type'> 类型:<class 'type'> 地址:10289024
---------------------------
Student的父类: (<class 'object'>,)
object的父类: ()
type的父类: (<class 'object'>,)
从结果中可以看出:
所有类(包括type)的父类都是object
所有类(包括object)都是type的实例
所以说:python中一切皆对象,因为所有类都是type的实例连type自己都是自己的实例。在内存中具有一块内存区域,有自己的类型和值。
2 ,type object class 关系
对象 object 是通过 class创建出来的 ,而 class 是通过 type创建出来的。
也就是说class是type的实例,而object是class的实例
type->class->object