这些模块都是在讲OOP时讲到的。

都是类中内置的。

#!/usr/bin/env python
# coding:utf-8 from lib.aa import C c1 = C()
print(c1.name) print(c1.__module__) # 来自哪个模块
print(c1.__class__) # 来自哪个类 class Foo:
'''这里是类的描述信息。。。'''
def __init__(self,name):
self.name = name def __del__(self): # 在程序执行完毕后,内存释放资源回收才会被执行
print('我执行了__del__') def __call__(self, *args, **kwargs): # 对象后面加括号,触发执行。
print('我执行了 __call__') f1 = Foo('abcde') del f1.name # 这个不会触发 __del__ print('=========')
f1()
# Foo('tom')
print(f1.__doc__)

自定义的格式化方法:

#!/usr/bin/env python
# coding:utf-8 # 自定义的格式化
# x='{0}{0}{0}'.format('dog')
#
# print(x) # class Date:
# def __init__(self,year,mon,day):
# self.year=year
# self.mon=mon
# self.day=day
# d1=Date(2016,12,26)
#
# x='{0.year}{0.mon}{0.day}'.format(d1)
# y='{0.year}:{0.mon}:{0.day}'.format(d1)
# z='{0.mon}-{0.day}-{0.year}'.format(d1)
# print(x)
# print(y)
# print(z) # x='{0.year}{0.mon}{0.day}'.format(d1)
# y='{0.year}:{0.mon}:{0.day}'
# z='{0.mon}-{0.day}-{0.year}' format_dic={
'ymd':'{0.year}{0.mon}{0.day}',
'm-d-y':'{0.mon}-{0.day}-{0.year}',
'y/m/d':'{0.year}/{0.mon}/{0.day}'
}
class Date:
def __init__(self,year,mon,day):
self.year=year
self.mon=mon
self.day=day
def __format__(self, format_spec): # 自定制的format方法
# print('我执行啦')
# print('--->',format_spec)
if not format_spec or format_spec not in format_dic:
format_spec='y/m/d'
fm=format_dic[format_spec]
return fm.format(self) d1=Date(2018,2,16)
# format(d1) #d1.__format__()
# print(format(d1))
print(format(d1,'ymd'))
print(format(d1,'y:m:d'))
print(format(d1,'m-d-y'))
print(format(d1,'m-d:y'))
print('===========>',format(d1,''))

三个 item 的方式,区别于 attr 的方式

#!/usr/bin/env python
# coding:utf-8 class Foo:
def __getitem__(self, item):
print('getitem')
return self.__dict__[item] def __setitem__(self, key, value):
print('setitem')
self.__dict__[key]=value def __delitem__(self, key):
print('delitem')
self.__dict__.pop(key) f1 = Foo()
## 点的方式操作属性和字典方式操作的区别是调用不同的内部方法 f1.age = 18 # 使用点的方式不会触发__setitem__ 只会触发 __setattr__
f1['name']='alex' # 使用字典方式调用时才会触发的方法
f1['gender']='male' print(f1.age) # 不会触发 __getitem__ 方法
print(f1['age']) del f1['gender'] print(f1.__dict__)
#!/usr/bin/env python
# coding:utf-8 class Foo:
def __init__(self,name,age):
self.name = name
self.age =age # def __str__(self): # 自定制的对象显示方式
# return '名字是%s 年龄是%s' %(self.name, self.age) def __repr__(self): # 自定制的对象显示方式
return '名字%s 年龄%s' %(self.name, self.age) # str是在print时显示,repr是在解释器中显示 f1 = Foo('alex',18)
print(f1) # 实际上是在触发 __str__ 如果找不到,就会去找 __repr__ 来代替

使对象可迭代:

#!/usr/bin/env python
# coding:utf-8 # 使对象可迭代
class Foo:
def __init__(self,n):
self.n=n
def __iter__(self):
return self def __next__(self):
if self.n == 13:
raise StopIteration('终止了')
self.n+=1
return self.n # l=list('hello')
# for i in l:
# print(i)
f1=Foo(10)
# print(f1.__next__())
# print(f1.__next__())
# print(f1.__next__())
# print(f1.__next__()) for i in f1: # obj=iter(f1)------------>f1.__iter__()
print(i) #obj.__next_() class Fib:
def __init__(self):
self._a=1
self._b=1 def __iter__(self):
return self
def __next__(self):
if self._a > 100:
raise StopIteration('终止了')
self._a,self._b=self._b,self._a + self._b
return self._a f1=Fib()
print(next(f1))
print(next(f1))
print(next(f1))
print(next(f1))
print(next(f1))
print('==================================')
for i in f1:
print(i)

减少内存消耗: __slots__

#!/usr/bin/env python
# coding:utf-8 class Foo: def __init__(self,name,age):
self.name = name
self.age =age # 取消了所有实例的__dict__ 优势是节省内存。 附加优势是限制了属性
# __slots__ = 'name'
__slots__ = ['name','age'] f1 = Foo('alex',18)
# print(f1.__dict__) 出错 print(f1.__slots__)
print(f1.name,f1.age) # 参考: https://www.cnblogs.com/rainfd/p/slots.html

描述符:

#!/usr/bin/env python
# coding:utf-8 class Foo:
def __get__(self, instance, owner):
print('===>get方法')
def __set__(self, instance, value):
print('===>set方法',instance,value)
instance.__dict__['x']=value #b1.__dict__
def __delete__(self, instance):
print('===>delete方法') class Bar:
x=Foo() #在何地?
def __init__(self,n):
self.x=n #b1.x=10
b1=Bar(10)
print(b1.__dict__)
b1.x=11111111111111
print(b1.__dict__) b1.y=11111111111111111111
print(b1.__dict__) # 参考: https://www.cnblogs.com/wangyongsong/p/6769256.html # print(Bar.__dict__)
#在何时?
# b1=Bar()
# b1.x
#
# b1.x=1
#
# del b1.x # print(b1.x)
#
# b1.x=1
# print(b1.__dict__)
#
# del b1.x

通常, 大家都是用的pip 或 pip3 来安装相应模块的。

但是,pip的官方仓库,经常响应太慢。很容易timeout.

所以,参照网友的方法: 修改成国内的pip源。比如下面的是豆瓣的源,在国内南方响应较快。

以我win10 为例: 运行, %Appdata%  然后在打开的目录下新建文件夹pip   打开pip  ,在下面新建pip.ini文件 ,贴上下面的内容保存即可。

[global]
trusted-global=pypi.douban.com
index-url = https://pypi.douban.com/simple
[install]
trusted-host = pypi.doubanio.com

现在使用pip install 就快多了。

python模块之_pip_其它的更多相关文章

  1. 使用C/C++写Python模块

    最近看开源项目时学习了一下用C/C++写python模块,顺便把学习进行一下总结,废话少说直接开始: 环境:windows.python2.78.VS2010或MingW 1 创建VC工程 (1) 打 ...

  2. Python模块之configpraser

    Python模块之configpraser   一. configpraser简介 用于处理特定格式的文件,其本质还是利用open来操作文件. 配置文件的格式: 使用"[]"内包含 ...

  3. Python模块之"prettytable"

    Python模块之"prettytable" 摘要: Python通过prettytable模块可以将输出内容如表格方式整齐的输出.(对于用Python操作数据库会经常用到) 1. ...

  4. python 学习第五天,python模块

    一,Python的模块导入 1,在写python的模块导入之前,先来讲一些Python中的概念性的问题 (1)模块:用来从逻辑上组织Python代码(变量,函数,类,逻辑:实现一个功能),本质是.py ...

  5. windows下安装python模块

    如何在windows下安装python模块 1. 官网下载安装包,比如(pip : https://pypi.python.org/pypi/pip#downloads) pip-9.0.1.tar. ...

  6. 安装第三方Python模块,增加InfoPi的健壮性

    这3个第三方Python模块是可选的,不安装的话InfoPi也可以运行. 但是如果安装了,会增加InfoPi的健壮性. 目录 1.cchardet    自动检测文本编码 2.lxml    用于解析 ...

  7. Python基础篇【第5篇】: Python模块基础(一)

    模块 简介 在计算机程序的开发过程中,随着程序代码越写越多,在一个文件里代码就会越来越长,越来越不容易维护. 为了编写可维护的代码,我们把很多函数分组,分别放到不同的文件里,这样,每个文件包含的代码就 ...

  8. python 模块加载

    python 模块加载 本文主要介绍python模块加载的过程. module的组成 所有的module都是由对象和对象之间的关系组成. type和object python中所有的东西都是对象,分为 ...

  9. pycharm安装python模块

    这个工具真的好好,真的很喜欢,它很方便,很漂亮,各种好 pycharm安装python模块:file-setting-搜索project inte OK

随机推荐

  1. vjson.hpp

    //vov #ifndef VJSON_HPP #define VJSON_HPP #include <iostream> #include <string> #include ...

  2. 在Vue项目中使用Element UI:按需引入和完整引入

    下面操作在main.js文件中进行 完整引入: import Element from 'element-ui'; //样式文件,需单独引入 import 'element-ui/lib/theme- ...

  3. java,桶排序,冒泡排序,快速排序

    1.桶排序: 百度百科:桶排序 (Bucket sort)或所谓的箱排序,是一个排序算法,工作的原理是将数组分到有限数量的桶子里.每个桶子再个别排序(有可能再使用别的排序算法或是以递归方式继续使用桶排 ...

  4. jacoco覆盖率工具测试及性能分析

    ant版本:https://ant.apache.org/bindownload.cgi jdk版本 注: ant 1.10    --->   jdk1.8 ant 1.9      ---& ...

  5. SWUST OJ(1028)

    特定字符序列的判断 #include <iostream> #include <cstdlib> #include <stack> #include <str ...

  6. OO第二单元多线程电梯总结分析

    一.概述 这一部分的作业考察的关注点与上一次的作业有所不同,上一次的考察重点主要集中在输入输出的判定以及多态的考察上面,而这一次是让我们进行多线程程序的调度与开发.这次开发过程中最大的感受就是自己之前 ...

  7. 新手vue构建单页面应用实例

    本人写的小程序,功能还在完善中,欢迎扫一扫提出宝贵意见! 步骤: 1.使用vue-cli创建项目2.使用vue-router实现单页路由3.用vuex管理我们的数据流4.使用vue-resource请 ...

  8. ES5 & ES6 基础

    一.什么是ES5 附上一览表 (5.1中文 (2011.6)): http://lzw.me/pages/ecmascript/ (5.1英文PDF):http://www.ecma-internat ...

  9. 二进制转base64

    一. 以fetch的获取数据 1. response(后台返回): const buffer = response.arrayBuffer(),将二级制转成arrayBuffer类型 2. buffe ...

  10. event flow

    JS之event flow DOM事件流 1.定义: DOM(文档对象模型)结构是一个树型结构,当一个HTML元素产生一个事件时,该事件会在元素节点与根结点之间的路径传播,路径所经过的结点都会收到该事 ...