两点水

pull/1/head
347073565@qq.com 2017-12-22 16:14:43 +08:00
parent fdbf518c1e
commit 1b69364df2
24 changed files with 53 additions and 54 deletions

View File

@ -67,7 +67,7 @@
- [使用 `type()` 动态创建类](/python12/2.md)
- [什么是元类](/python12/3.md)
- [自定义元类](/python12/4.md)
- [使用元类](/python12/4.md)
- [使用元类](/python12/5.md)
* [线程与进程](/python13/Preface.md)
- [线程与进程](/python13/1.md)
- [多线程编程](/python13/2.md)

View File

@ -22,6 +22,6 @@ if __name__ == '__main__':
输出的结果:
![Python 类的魔术方法](https://user-gold-cdn.xitu.io/2017/8/7/b8baa846d8b0f968b14e1e485afb239d)
![Python 类的魔术方法](http://p1ceh5usj.bkt.clouddn.com/Python%20%E7%B1%BB%E7%9A%84%E9%AD%94%E6%9C%AF%E6%96%B9%E6%B3%95.png)
可以看到,一个类的魔术方法还是挺多的,截图也没有截全,不过我们只需要了解一些常见和常用的魔术方法就好了。

View File

@ -16,7 +16,7 @@ user=User('两点水',23)
实际上,创建一个类的过程是分为两步的,一步是创建类的对象,还有一步就是对类进行初始化。`__new__` 是用来创建类并返回这个类的实例, 而`__init__` 只是将传入的参数来初始化该实例.`__new__` 在创建一个实例的过程中必定会被调用,但 `__init__` 就不一定比如通过pickle.load 的方式反序列化一个实例时就不会调用 `__init__` 方法。
![Python类创建的过程](https://user-gold-cdn.xitu.io/2017/8/3/dd588107e7e243bfbe11dc517fdb5308)
![Python类创建的过程](http://upload-images.jianshu.io/upload_images/2136918-a2b39b078cc81841?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
`def __new__(cls)` 是在 `def __init__(self)` 方法之前调用的,作用是返回一个实例对象。还有一点需要注意的是:`__new__` 方法总是需要返回该类的一个实例,而 `__init__` 不能返回除了 `None` 的任何值

View File

@ -109,4 +109,4 @@ num1 >= num2 ? --------> False
最后,如果对本文感兴趣的,可以关注下公众号:
![公众号](https://user-gold-cdn.xitu.io/2017/7/31/dc2347facde1d4b004faef7de0042b2a)
![公众号](http://twowater.com.cn/images/20171204192251900.gif)

View File

@ -4,4 +4,4 @@
# 目录 #
![草根学Python Python 的 Magic Method](https://user-gold-cdn.xitu.io/2017/8/17/de0e1491506e81159ff4c6bdece91c73)
![草根学Python Python 的 Magic Method](http://p1ceh5usj.bkt.clouddn.com/%E8%8D%89%E6%A0%B9%E5%AD%A6Python%EF%BC%88%E5%8D%81%EF%BC%89%20Python%20%E7%9A%84%20Magic%20Method.png)

View File

@ -37,6 +37,6 @@ print('\n', Month.Jan)
输出的结果如下:
![Python3 枚举类型的使用](https://user-gold-cdn.xitu.io/2017/8/20/68bc0c10a2f7026fdcb4b0e7230d5db6)
![Python3 枚举类型的使用](http://p1ceh5usj.bkt.clouddn.com/Python3%20%E6%9E%9A%E4%B8%BE%E7%B1%BB%E5%9E%8B%E7%9A%84%E4%BD%BF%E7%94%A8.png)
可见,我们可以直接使用 `Enum` 来定义一个枚举类。上面的代码,我们创建了一个有关月份的枚举类型 Month ,这里要注意的是构造参数,第一个参数 Month 表示的是该枚举类的类名,第二个 tuple 参数,表示的是枚举类的值;当然,枚举类通过 `__members__` 遍历它的所有成员的方法。注意的一点是 `member.value` 是自动赋给成员的 `int`类型的常量,默认是从 1 开始的。而且 Enum 的成员均为单例Singleton并且不可实例化不可更改

View File

@ -38,7 +38,7 @@ if __name__ == '__main__':
输出的结果如下:
![Python3 自定义类型的枚举类](https://user-gold-cdn.xitu.io/2017/8/20/a59e989e702ccddfa5539b16fdcffa02)
![Python3 自定义类型的枚举类](http://p1ceh5usj.bkt.clouddn.com/Python3%20%E8%87%AA%E5%AE%9A%E4%B9%89%E7%B1%BB%E5%9E%8B%E7%9A%84%E6%9E%9A%E4%B8%BE%E7%B1%BB.png)

View File

@ -6,4 +6,4 @@
# 目录 #
![草根学Python十一 枚举类](https://user-gold-cdn.xitu.io/2017/8/28/e403eb0edf80f951450edfef8e306a2f)
![草根学Python十一 枚举类](http://p1ceh5usj.bkt.clouddn.com/%E8%8D%89%E6%A0%B9%E5%AD%A6Python%EF%BC%88%E5%8D%81%E4%B8%80%EF%BC%89%20%E6%9E%9A%E4%B8%BE%E7%B1%BB.png)

View File

@ -30,4 +30,4 @@ Python 中的一切都是对象,它们要么是类的实例,要么是元类
最后如果对本文有兴趣,可以关注公众号:
![公众号](https://user-gold-cdn.xitu.io/2017/7/31/dc2347facde1d4b004faef7de0042b2a)
![公众号](http://twowater.com.cn/images/20171204192251900.gif)

View File

@ -4,4 +4,4 @@
# 目录 #
![草根学Python十二元类](https://user-gold-cdn.xitu.io/2017/9/7/375f11ec2c4b80ad661187bc63ed8913)
![草根学Python十二元类](http://p1ceh5usj.bkt.clouddn.com/%E8%8D%89%E6%A0%B9%E5%AD%A6Python%EF%BC%88%E5%8D%81%E4%BA%8C%EF%BC%89%20%E5%85%83%E7%B1%BB.png)

View File

@ -182,7 +182,7 @@ r_lock = threading.RLock()
其中条件变量可以看成不同的线程先后 acquire 获得锁,如果不满足条件,可以理解为被扔到一个( Lock 或 RLock )的 waiting 池。直达其他线程 notify 之后再重新判断条件。不断的重复这一过程,从而解决复杂的同步问题。
![Condition](https://user-gold-cdn.xitu.io/2017/9/29/16cb2982e24b5c5707b8dc73a981d2f3)
![Condition](http://p1ceh5usj.bkt.clouddn.com/Condition.png)
该模式常用于生产者消费者模式,具体看看下面在线购物买家和卖家的示例:

View File

@ -47,7 +47,7 @@ if __name__ == "__main__":
输出的结果:
![多进程输出结果](https://user-gold-cdn.xitu.io/2017/10/9/8173ebe5bc7c20144917417b8bb7b981)
![多进程输出结果](http://p1ceh5usj.bkt.clouddn.com/%E5%A4%9A%E8%BF%9B%E7%A8%8B%E8%BE%93%E5%87%BA%E7%BB%93%E6%9E%9C.gif)
## 2、把进程创建成类 ##
@ -83,8 +83,7 @@ if __name__ == '__main__':
输出结果如下:
![创建进程类](https://user-gold-cdn.xitu.io/2017/10/9/e3d45d136c05fda784364eae221e7fef)
![创建进程类](http://p1ceh5usj.bkt.clouddn.com/%E5%88%9B%E5%BB%BA%E8%BF%9B%E7%A8%8B%E7%B1%BB.gif)
## 3、daemon 属性 ##
@ -310,4 +309,3 @@ if __name__ == '__main__':
写进 Queue 的值为:四点水
从 Queue 读取的值为:四点水
```

View File

@ -4,4 +4,4 @@
# 目录 #
![草根学Python十三 线程和进程](https://user-gold-cdn.xitu.io/2017/10/13/3e04bf065d2da8440f64e2f0e0af8b06)
![草根学Python十三 线程和进程](http://p1ceh5usj.bkt.clouddn.com/%E8%8D%89%E6%A0%B9%E5%AD%A6Python%EF%BC%88%E5%8D%81%E4%B8%89%EF%BC%89%20%E7%BA%BF%E7%A8%8B%E5%92%8C%E8%BF%9B%E7%A8%8B.png)

View File

@ -16,6 +16,6 @@
Python 本身就内置了很多非常有用的模块,只要安装完毕,这些模块就可以立刻使用。我们可以尝试找下这些模块,比如我的 Python 安装目录是默认的安装目录,在 C:\Users\Administrator\AppData\Local\Programs\Python\Python36 ,然后找到 Lib 目录,就可以发现里面全部都是模块,没错,这些 `.py` 文件就是模块了。
![python36bin目录](https://user-gold-cdn.xitu.io/2017/7/10/9bfc6a185ef8faf2bd312687f2c538e7)
![python36bin目录](http://upload-images.jianshu.io/upload_images/2136918-5ecc6493206da1ec?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
其实模块可以分为标准库模块和自定义模块,而刚刚我们看到的 Lib 目录下的都是标准库模块。

View File

@ -73,11 +73,12 @@ from modname import name1[, name2[, ... nameN]]
`import` 导入 sys 模块,然后使用 version 属性
![from···import和 import的区别1](https://user-gold-cdn.xitu.io/2017/7/10/624b47207a611821ef4f887e28f11372)
![from···import和 import的区别1](http://upload-images.jianshu.io/upload_images/2136918-499dd531d4ce3d72?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
`from···import` 直接导入 version 属性
![from···import和 import的区别2](https://user-gold-cdn.xitu.io/2017/7/10/a7519d59c263ed48b74ec043f9e02de2)
![from···import和 import的区别2](http://upload-images.jianshu.io/upload_images/2136918-eea99fc170ed5a07?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
## 3、from ··· import * ##

View File

@ -14,9 +14,10 @@
首先创建了模块 lname ,然后判断一下是否是主模块,如果是主模块就输出 `main` 不是,就输出 `not main` ,首先直接运行该模块,由于该模块是直接使用,而没有被人调用,所以是主模块,因此输出了 `main` ,具体看下图:
![name属性区分模块1](https://user-gold-cdn.xitu.io/2017/7/10/fa532dbb04f61dcf755f820e2f81dbed)
![name属性区分模块1](http://upload-images.jianshu.io/upload_images/2136918-d892d81a71cda9d6?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
然后又创建一个 user_lname 模块,里面只是简单的导入了 lname 模块,然后执行,输出的结果是 `not main` ,因为 lname 模块被该模块调用了,所以不是主模块,输出结果如图:
![name属性区分模块2](https://user-gold-cdn.xitu.io/2017/7/10/8fc6e084c1962ebc3c9dee47a7656b4f)
![name属性区分模块2](http://upload-images.jianshu.io/upload_images/2136918-9137cb874588dded?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

View File

@ -4,7 +4,7 @@
比如最开始的例子,就引入了包,这样子做就算有相同的模块名,也不会造成重复,因为包名不同,其实也就是路径不同。如下图,引入了包名后, lname.py 其实变成了 com.Learn.module.nameattributes.lname
![Python 包](https://user-gold-cdn.xitu.io/2017/7/11/012a0f3883a9b1414af5580a4c01cb97)
![Python 包](http://upload-images.jianshu.io/upload_images/2136918-7f92a0da0bc609d5?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
仔细观察的人,基本会发现,每一个包目录下面都会有一个 `__init__.py` 的文件,为什么呢?

View File

@ -4,4 +4,4 @@
# 目录 #
![草根学Python 模块与包](https://user-gold-cdn.xitu.io/2017/7/12/cec403e8ec56c1f17b466e7488f5ed23)
![草根学Python 模块与包](http://upload-images.jianshu.io/upload_images/2136918-4434f73dc82c0101?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

View File

@ -52,7 +52,7 @@ t.prt()
观察输出的结果:
![Python self](https://user-gold-cdn.xitu.io/2017/7/30/933b74a7ae0e312c40806ea8dad092d4)
![Python self](http://upload-images.jianshu.io/upload_images/2136918-66d29b081ad5510b?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
从执行结果可以很明显的看出self 代表的是类的实例,输出的是当前对象的地址,而 `self.__class__` 则指向类。
@ -115,4 +115,3 @@ if __name__ == '__main__':
仔细观察输出的结果对比一下就能观察出来注意喔Pyhton3 中输出的结果是一模一样的因为Python3 中没有新式类旧式类的问题。

View File

@ -27,7 +27,7 @@ class UserInfo(object):
为什么只能说一般情况下呢?因为实际上, Python 中是没有提供私有属性等功能的。但是 Python 对属性的访问控制是靠程序员自觉的。为什么这么说呢?看看下面的示例:
![Python 属性访问控制](https://user-gold-cdn.xitu.io/2017/7/28/cf3ef904a9c84bd15245cd1f0e1f3e88)
![Python 属性访问控制](http://upload-images.jianshu.io/upload_images/2136918-4ac2643aa5b0fa37?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
仔细看图片,为什么说双下划线不是真正的私有属性呢?我们看下下面的例子,用下面的例子来验证:
@ -61,4 +61,4 @@ if __name__ == '__main__':
输出的结果如下图:
![Python 属性访问控制](https://user-gold-cdn.xitu.io/2017/7/28/b147b0390a9b4cbcd5765658b54c12e9)
![Python 属性访问控制](http://upload-images.jianshu.io/upload_images/2136918-de89d4dc5f5f9f77?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

View File

@ -103,4 +103,4 @@ if __name__ == '__main__':
运行的结果:
![Python 方法的装饰器](https://user-gold-cdn.xitu.io/2017/7/29/94ae6aec01a94136951cbb8314008781)
![Python 方法的装饰器](http://upload-images.jianshu.io/upload_images/2136918-63dc478a8b2f965f?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

View File

@ -112,7 +112,7 @@ if __name__ == '__main__':
最后打印的结果:
![Python 类的继承](https://user-gold-cdn.xitu.io/2017/7/29/a953f75e593c5a0586b06a28abc323d9)
![Python 类的继承](http://upload-images.jianshu.io/upload_images/2136918-aa2701fc5913a8a6?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
这里就是重写了父类的构造函数。

View File

@ -7,4 +7,4 @@
# 目录 #
![草根学Python面向对象](https://user-gold-cdn.xitu.io/2017/7/30/4c851ddc1fd9de58d7d7f205b8010c6d)
![草根学Python面向对象](http://upload-images.jianshu.io/upload_images/2136918-eecf427fdbd1688c?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)