Skip to content

Commit aaf697d

Browse files
committed
star
1 parent 9762d0d commit aaf697d

32 files changed

Lines changed: 647 additions & 2 deletions

feature.md

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ Python小例子 https://github.com/jackzhenguo/python-small-examples
3939

4040
## 小例子
4141

42-
| ID | md文件链接 | 难度 |
42+
| ID | md 例子链接 | 难度 |
4343
| ---- | ---------------------------------- | ---- |
4444
| 1 | [求绝对值](md/1.md) | ⭐️ |
4545
| 2 | [进制转化](md/2.md) | ⭐️⭐️ |
@@ -85,4 +85,35 @@ Python小例子 https://github.com/jackzhenguo/python-small-examples
8585
| 42 | [issubclass父子关系鉴定](md/42.md) | ⭐️⭐️⭐️ |
8686
| 43 | [所有对象之根](md/43.md) | ⭐️ |
8787
| 44 | [创建属性的两种方法](md/44.md) | ⭐️⭐️⭐️⭐️⭐️ |
88-
| 45 | [查看对象类型](md/45.md) | ⭐️ |
88+
| 45 | [查看对象类型](md/45.md) | ⭐️ |
89+
| 46 | [元类使用介绍](md/46.md) | ⭐️⭐️⭐️⭐️⭐️ |
90+
| 47 | [枚举对象](md/47.md) | ⭐️⭐️⭐️ |
91+
| 48 | [查看变量所占字节数](md/48.md) | ⭐️⭐️⭐️ |
92+
| 49 | [过滤器filter](md/49.md) | ⭐️⭐️⭐️ |
93+
| 50 | [返回对象哈希值](md/50.md) | ⭐️⭐️ |
94+
| 51 | [help 一键帮助](md/51.md) | ⭐️ |
95+
| 52 | [获取用户输入](md/52.md) | ⭐️ |
96+
| 53 | [创建迭代器](md/53.md) | ⭐️⭐️⭐️ |
97+
| 54 | [文件读写和mode 取值表](md/54.md) | ⭐️⭐️⭐️ |
98+
| 55 | [创建range序列](md/55.md) | ⭐️⭐️ |
99+
| 56 | [反向迭代器reversed](md/56.md) | ⭐️⭐️ |
100+
| 57 | [zip迭代器](md/57.md) | ⭐️⭐️⭐️ |
101+
| 58 | [operator使用举例](md/58.md) | ⭐️⭐️⭐️⭐️ |
102+
| 59 | [传输json对象](md/59.md) | ⭐️⭐️⭐️⭐️⭐️ |
103+
| 60 | [不用else和if实现计算器](md/60.md) | ⭐️⭐️⭐️ |
104+
| 61 | [去最求平均](md/61.md) | ⭐️⭐️⭐️⭐️ |
105+
| 62 | [打印99乘法表](md/62.md) | ⭐️⭐️⭐️ |
106+
| 63 | [递归版flatten函数](md/63.md) | ⭐️⭐️⭐️⭐️ |
107+
| 64 | [列表等分为n份](md/64.md) | ⭐️⭐️⭐️ |
108+
| 65 | [压缩列表](md/65.md) | ⭐️⭐️⭐️⭐️ |
109+
| 66 | [求更长的列表](md/66.md) | ⭐️⭐️⭐️⭐️⭐️ |
110+
| 67 | [求列表众数](md/67.md) | ⭐️⭐️⭐️⭐️ |
111+
| 68 | [所有多个列表的最大值](md/68.md) | ⭐️⭐️⭐️⭐️ |
112+
| 69 | [列表检查重复](md/69.md) | ⭐️⭐️⭐️ |
113+
| 70 | [一行代码实现列表反转](md/70.md) | ⭐️⭐️ |
114+
| 71 | [浮点数等差数列](md/71.md) | ⭐️⭐️⭐️⭐️ |
115+
| 72 | [按条件分组](md/72.md) | ⭐️⭐️⭐️⭐️ |
116+
| 73 | [map实现向量运算](md/73.md) | ⭐️⭐️⭐️ |
117+
| 74 | [值最大的字典](md/74.md) | ⭐️⭐️⭐️⭐️ |
118+
| 75 | [合并两个字典](md/75.md) | ⭐️⭐️⭐️ |
119+
| 76 | [Topn 字典](md/76.md) | ⭐️⭐️⭐️ |

md/46.md

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#### 46 元类使用介绍
2+
3+
`xiaoming`, `xiaohong`, `xiaozhang` 都是学生,这类群体叫做 `Student`.
4+
5+
Python 定义类的常见方法,使用关键字 `class`
6+
7+
```python
8+
In [36]: class Student(object):
9+
...: pass
10+
```
11+
12+
`xiaoming`, `xiaohong`, `xiaozhang` 是类的实例,则:
13+
14+
```python
15+
xiaoming = Student()
16+
xiaohong = Student()
17+
xiaozhang = Student()
18+
```
19+
20+
创建后,xiaoming 的 `__class__` 属性,返回的便是 `Student`
21+
22+
```python
23+
In [38]: xiaoming.__class__
24+
Out[38]: __main__.Student
25+
```
26+
27+
问题在于,`Student` 类有 `__class__`属性,如果有,返回的又是什么?
28+
29+
```python
30+
In [39]: xiaoming.__class__.__class__
31+
Out[39]: type
32+
```
33+
34+
哇,程序没报错,返回 `type`
35+
36+
那么,我们不妨猜测:`Student` 类,类型就是 `type`
37+
38+
换句话说,`Student`类就是一个**对象**,它的类型就是 `type`
39+
40+
所以,Python 中一切皆对象,**类也是对象**
41+
42+
Python 中,将描述 `Student` 类的类被称为:元类。
43+
44+
按照此逻辑延伸,描述元类的类被称为:*元元类*,开玩笑了~ 描述元类的类也被称为元类。
45+
46+
聪明的朋友会问了,既然 `Student` 类可创建实例,那么 `type` 类可创建实例吗? 如果能,它创建的实例就叫:类 了。 你们真聪明!
47+
48+
说对了,`type` 类一定能创建实例,比如 `Student` 类了。
49+
50+
```python
51+
In [40]: Student = type('Student',(),{})
52+
53+
In [41]: Student
54+
Out[41]: __main__.Student
55+
```
56+
57+
它与使用 `class` 关键字创建的 `Student` 类一模一样。
58+
59+
Python 的类,因为又是对象,所以和 `xiaoming``xiaohong` 对象操作相似。支持:
60+
61+
- 赋值
62+
- 拷贝
63+
- 添加属性
64+
- 作为函数参数
65+
66+
```python
67+
In [43]: StudentMirror = Student # 类直接赋值 # 类直接赋值
68+
In [44]: Student.class_property = 'class_property' # 添加类属性
69+
In [46]: hasattr(Student, 'class_property')
70+
Out[46]: True
71+
```
72+
73+
元类,确实使用不是那么多,也许先了解这些,就能应付一些场合。就连 Python 界的领袖 `Tim Peters` 都说:
74+
75+
“元类就是深度的魔法,99%的用户应该根本不必为此操心。

md/47.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#### 47 枚举对象  
2+
3+
返回一个可以枚举的对象,该对象的next()方法将返回一个元组。
4+
5+
```python
6+
In [1]: s = ["a","b","c"]
7+
...: for i ,v in enumerate(s,1):
8+
...: print(i,v)
9+
...:
10+
1 a
11+
2 b
12+
3 c
13+
```

md/48.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#### 48 查看变量所占字节数
2+
3+
```python
4+
In [1]: import sys
5+
6+
In [2]: a = {'a':1,'b':2.0}
7+
8+
In [3]: sys.getsizeof(a) # 占用240个字节
9+
Out[3]: 240
10+
```

md/49.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#### 50 过滤器  
2+
3+
在函数中设定过滤条件,迭代元素,保留返回值为`True`的元素:
4+
5+
```python
6+
In [1]: fil = filter(lambda x: x>10,[1,11,2,45,7,6,13])
7+
8+
In [2]: list(fil)
9+
Out[2]: [11, 45, 13]
10+
```

md/50.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#### 50 返回对象哈希值  
2+
3+
返回对象的哈希值,值得注意的是自定义的实例都是可哈希的,`list`, `dict`, `set`等可变对象都是不可哈希的(unhashable)
4+
5+
```python
6+
In [1]: hash(xiaoming)
7+
Out[1]: 6139638
8+
9+
In [2]: hash([1,2,3])
10+
# TypeError: unhashable type: 'list'
11+
```

md/51.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#### 51 help 一键帮助 
2+
3+
返回对象的帮助文档
4+
5+
```python
6+
In [1]: help(xiaoming)
7+
Help on Student in module __main__ object:
8+
9+
class Student(builtins.object)
10+
| Methods defined here:
11+
|
12+
| __init__(self, id, name)
13+
|
14+
| __repr__(self)
15+
|
16+
| Data descriptors defined here:
17+
|
18+
| __dict__
19+
| dictionary for instance variables (if defined)
20+
|
21+
| __weakref__
22+
| list of weak references to the object (if defined)
23+
```

md/52.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
### 52 获取用户输入 
2+
3+
获取用户输入内容
4+
5+
```python
6+
In [1]: input()
7+
aa
8+
Out[1]: 'aa'
9+
```

md/53.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#### 53 创建迭代器
2+
3+
使用`iter(obj, sentinel)`, 返回一个可迭代对象, sentinel可省略(一旦迭代到此元素,立即终止)
4+
5+
```python
6+
In [1]: lst = [1,3,5]
7+
8+
In [2]: for i in iter(lst):
9+
...: print(i)
10+
...:
11+
1
12+
3
13+
5
14+
```
15+
16+
```python
17+
In [1]: class TestIter(object):
18+
...: def __init__(self):
19+
...: self.l=[1,3,2,3,4,5]
20+
...: self.i=iter(self.l)
21+
...: def __call__(self): #定义了__call__方法的类的实例是可调用的
22+
...: item = next(self.i)
23+
...: print ("__call__ is called,fowhich would return",item)
24+
...: return item
25+
...: def __iter__(self): #支持迭代协议(即定义有__iter__()函数)
26+
...: print ("__iter__ is called!!")
27+
...: return iter(self.l)
28+
In [2]: t = TestIter()
29+
In [3]: t() # 因为实现了__call__,所以t实例能被调用
30+
__call__ is called,which would return 1
31+
Out[3]: 1
32+
33+
In [4]: for e in TestIter(): # 因为实现了__iter__方法,所以t能被迭代
34+
...: print(e)
35+
...:
36+
__iter__ is called!!
37+
1
38+
3
39+
2
40+
3
41+
4
42+
5
43+
```

md/54.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#### 54 文件读写和mode 取值表
2+
3+
返回文件对象
4+
5+
```python
6+
In [1]: fo = open('D:/a.txt',mode='r', encoding='utf-8')
7+
8+
In [2]: fo.read()
9+
Out[2]: '\ufefflife is not so long,\nI use Python to play.'
10+
In [3]: fo.close() # 关闭文件对象
11+
```
12+
13+
mode 取值表:
14+
15+
| 字符 | 意义 |
16+
| :---- | :------------------------------- |
17+
| `'r'` | 读取(默认) |
18+
| `'w'` | 写入,并先截断文件 |
19+
| `'x'` | 排它性创建,如果文件已存在则失败 |
20+
| `'a'` | 写入,如果文件存在则在末尾追加 |
21+
| `'b'` | 二进制模式 |
22+
| `'t'` | 文本模式(默认) |
23+
| `'+'` | 打开用于更新(读取与写入) |

0 commit comments

Comments
 (0)