python模块之_pip_其它

这些模块都是在讲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,'123'))

三个 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 就快多了。

原文地址:https://www.cnblogs.com/frx9527/p/python__.html