1. 一切皆对象
函数, 类 都是对象
特性:
- 赋值给一个变量
- 可以添加到集合对象中
- 可以作为参数传递给函数
- 可以当作函数的返回值
代码举例:
函数:
>>> def ask(name='cannon1'):
>>> print(name)
>>> my_func = ask
>>> my_func('shenjianeng')
shenjianeng
类:
>>> class Person:
>>> def __init__(self):
>>> print('cannon2')
>>> my_class = Person
>>> my_class()
cannon2
集合:
>>> obj_list = []
>>> obj_list.append(ask)
>>> obj_list.append(Person)
>>> for item in obj_list:
>>> print(item())
cannon1
None # def 没有返回值
cannon2
<__main__.Person object at 0x109cf02e8>
函数返回函数:
>>> def decorator_func():
>>> print('dec start')
>>> return ask
>>> my_ask = decorator_func()
>>> my_ask('my ask')
dec start
my ask
2. type object 和 class之间的关系
type->int->1
type->class->obj
>>> a=1
>>> type(1)
<class 'int'>
>>> type(int)
<class 'type'>
>>> stu=student()
>>> type(stu)
<class '__main__.student'>
>>> type(student)
<class 'type'>
object是最顶层的父类
type也是一个类,同时也是一个对象
>>> Mystudent.__bases__
(<class '__main__.student'>,)
>>> type.__bases__
(<class 'object'>,)
>>> type(object)
<class 'type'>
>>> object.__bases__
()
3. python的内置类型
对象的三个特征:身份, 类型, 值
身份:
id可查看内存地址 即身份
>>> a=1
>>> id(a)
4352361808
类型:
- None
全局只有一个
>>> a=None
>>> b=None
>>> id(a)
4352081784
>>> id(b)
4352081784
数值
int float complex(复数) bool迭代类型
序列类型
list
bytes, bytearray, memoryview(二进制序列)
range
tuple
str
array映射(list)
集合 (set, frozenset)
上下文管理类型 (with)
其他
模块类型 (from import)
class和实例
函数类型
方法类型
代码类型
object对象
type类型
ellipsis类型(...)
notimplementd类对象