python

Python 高级核心

Python高级核心技术全套课程

python在linux安装。ln -s /aaaa /bbbb。

image-20251201135308697

01 一切皆对象

python的面向对象更加彻底

函数和类也是对象,是python中的一等公民:

1. 可以复制给变量
1. 可以添加到集合对象中
1. 可以作为参数传递给函数
1. 可以当做函数的返回值

type object class的关系

1
2
3
4
5
>>> a=1
>>> type(1)
<class 'int'>
>>> type(int)
<class 'type'>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
type有两种用法:
一:生成一个类-元类编程;
二:返回对应的一个对象的类型

a=1
type(1) >>> <class 'int'>
type(int) >>> <class 'type'>
int类是一个对象,1就是由这个类生成的;
同时int也是一个对象,是由type这个对象生成的。


type > int > 1
type > class > obj


常用的类对象,是由type这个类生成的;

object是最顶层的基类,所有的类都继承自object

type是一个类,同时type也是一个对象
type的基类指向object:type.__bases__ > class object, type的类型是type
object的类型是type:type(object) > class type,object的基类是空

object.__bases__ > () 空
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
### 基础定义
1.class:类,对象的模板,比如 str,int,list,或自定义对象Student; 定义对象的属性和方法,用于创建对象
2.object:python中所有对象的最终父类,顶层基类,所有对象都继承他的基础方法; 统一所有对象的底层接口,比如 __init__, __str__
3.type:元类metaclass,是类的类; 负责创建和管理类本身,类是type的实例
> 核心前提:在 Python 中,一切皆对象—— 不仅 123、"abc" 是对象,int、str 这些类,甚至 type、object 本身,都是对象!

### 关键结论
1.type是所有类的创造者,类是type的实例。
所有类,str,int,自定义类,都是type这个元类的实例;type是类工厂,专门生产类,再由类生产实例

~~~ python
# 1. 内置类(int、str、list)是 type 的实例
print(isinstance(int, type)) # True
print(isinstance(str, type)) # True
print(isinstance(list, type)) # True

# 2. 自定义类也是 type 的实例
class Person:
pass
print(isinstance(Person, type)) # True

# 3. 实例不是 type 的实例(实例是“类”的产物,不是“元类”的产物)
p = Person()
print(isinstance(p, type)) # False
~~~

2. object是所有对象的最终个父类,包括类和实例
所有对象(类+实例)都直接或间接继承自object
# 1. 所有类都继承自 object
print(issubclass(int, object)) # True
print(issubclass(str, object)) # True
print(issubclass(Person, object))# True

# 2. 实例的 __class__.__bases__ 最终指向 object
p = Person()
print(Person.__bases__) # (<class 'object'>,) → Person 直接继承 object
print(int.__bases__) # (<class 'object'>,) → int 直接继承 object

# 3. object 没有父类(它是顶层)
print(object.__bases__) # ()
type 是 object 的子类(issubclass(type, object) → True):因为 type 本身是 “对象”,必须继承 object 的基础方法;
object 是 type 的实例(isinstance(object, type) → True):因为 object 是 “类”,而所有类都是 type 创造的。

3. type和object相互依赖(鸡生蛋)
# type 继承自 object
print(issubclass(type, object)) # True

# object 是 type 的实例
print(isinstance(object, type)) # True

┌─────────────┐ ┌─────────────┐
│ │ 实例化 │ │ 实例化 ┌─────────────┐
│ type ├─────────▶│ class ├─────────▶ instance │
│ (元类) │ │ (类模板) │ │ (对象实例) │
└──────┬──────┘ └──────┬──────┘ └─────────────┘
│ │
│ 继承 │ 继承
▼ ▼
┌─────────────┐ ┌─────────────┐
│ object │◀─────────│ object │
│ (顶层父类) │ │ (顶层父类) │
└─────────────┘ └─────────────┘

image-20251201145023557

type自己实例化了自己,是通过指针实现的。type是自举的,是创造链的起点,打开python的时候就已经存在了。

python中的内置类型

id()可以查看对象的地址。

对象是有类型的。

None:全局唯一

a=None

b=None

id(a) == id(b) True #id() 函数返回变量所指对象的「内存地址」

数值:int float bool

迭代类型:

序列类型:list bytes/batearray/memoriview range tuple str array

映射类型:dict

集合:set frozenset

上下文管理类型:with

其他:模块 class和实例 函数 方法 代码 object type ellipsis nottinplement

魔法方法详解

1
2
3
4
def __getitem__(self, item):
return self.employee[item]
有了这个魔法方案,就可以直接对对象使用for方法。
加了这个魔法函数,对象就会变为切片类型

image-20251201161430427

image-20251201161938145

使用ipython

python序列协议

类和对象

python是动态语言,强调鸭子特性。

实现了某个魔法方法,可以理解为实现了某种协议。

抽象基类abc:1.抽象基类无法实例化 2.抽奖积累的方法必须被覆盖

深入dict和set

迭代器和生成器

socket编程

对象引用、可变性、垃圾回收

元类变成

对线成

异步io和协程

syncio并发编程