1. 动态语言和静态语言的区别

  • python的面向对象更彻底
  • Java中object是class的一个实例
  • Python中class也是对象, 函数也是对象
  • Python中的代码和模块也是对象

2. python的一等公民

函数和类也是对象,属于python的一等公民

  1. 赋值给一个变量
  2. 可以添加到集合对象中
  3. 可以作为参数传递给函数
  4. 可以当做函数的返回值

对类进行实例化的时候返回的是一个类的对象

3. type,object,class之间的关系

3.1 type的两种用法

  • 生成一个类
  • 返回一个对象的类型

3.2 type->int->1

  • type->class->obj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# 1是通过int这个类实例化的对象, int是通过type这个类实例化的对象
>>> a = 1
>>> type(1)
<class 'int'>
>>> type(int)
<class 'type'>

>>> class Student:
... pass
...
>>> class MyStudent(Student):
... pass
...
>>> stu = Student()

>>> type(stu)
<class '__main__.Student'>
>>> type(Student)
<class 'type'>
>>> type(object)
<class 'type'>
>>> type(type)
<class 'type'>

>>> a = 'abc'
>>> type(a)
<class 'str'>
>>> type(str)
<class 'type'>
>>> type(type)
<class 'type'>

>>> Student.__bases__
(<class 'object'>,)
>>> MyStudent.__bases__
(<class '__main__.Student'>,)
>>> type.__bases__
(<class 'object'>,)
>>> object.__bases__
()

4. python中的内置函数

4.1 python中对象的三个特征

4.1.1 身份

对象在内存中的地址,可以通过id去查看

4.1.2 类型

int类型、字符串类型…

4.1.3 值

a = 1, 1是指这个对象的值, 1会被python的int类型进行封装, 然后使用a指向1这个对象

4.2 python中的常见内置类型

4.2.1 None(全局只有一个)

1
2
3
a = None
b = None
a和b都是指向同一个对象

4.2.2 数值

1
2
3
4
int 
float
complex
bool

4.2.3 迭代类型

4.2.4 序列类型

1
2
3
4
5
6
list 
bytesbytearraymemoryview(二进制序列)
range
tuple
str
array

4.2.5 映射(dict)

4.2.6 集合

1
2
set
frozenset

4.2.7 上下文管理类型(with)

4.2.8 其他

1
2
3
4
5
6
7
8
模块类型
class和实例函数类型
方法类型
代码类型
object对象
type类型
ellipsis类型
notimplemented类对象