- 博客(9)
- 收藏
- 关注
原创 Python 上下文管理器
###上下文管理器class Sample: def __enter__(self): print('enter') #获取资源 return self def __exit__(self, exc_type, exc_val, exc_tb): print('exit') def do_something(self): print('do something ')with Sample() .
2021-08-29 18:45:51
110
原创 Python中类方法,静态方法,实例化方法
'''静态方法 类方法'''class Date: def __init__(self,year,month,day): self.year=year self.month=month self.day=day def tommorow(self): self.tommorow+=1 @staticmethod def parse_from_string(date_str): yea.
2021-08-29 10:46:30
242
原创 Python 装饰器
定义:装饰器用于拓展原来函数功能的一种语法,返回新函数替换旧函数优点:在不更改原函数的前提下,给函数拓展新的功能语法:@#1.装饰器的原型def kuozhan(func): def newfunc(): print('蓬头垢面') func() print('容光焕发') return newfunc@kuozhandef func(): print('我是一名学生')func()# func=kuozhan(.
2021-08-28 19:37:25
167
原创 Python 魔法函数
###__add__ 魔法函数(与之相关的__radd__反向加法)'''触发时机:使用对象进行运算相加的时候自动触发功能:对象运算参数:二个对象参数返回值:运算后的值'''class MyClass(): def __init__(self,num): self.num=num #对象在 加号+ 的左侧时,自动触发 def __add__(self, other): ##self 接受的是对象 ##other 接受的是 .
2021-08-28 09:33:51
270
原创 Python 魔法函数 __call__
# ###___call__魔法函数'''触发时机:把对象当作函数调用的时候自动触发功能:模拟函数化操作参数:参数不固定,至少一个self参数返回值:看需求'''#(1).基本语法import mathclass MyClass(): def __call__(self): print('call方法被触发')obj=MyClass()obj()#(2)洗衣服的过程class Wash(): def __call__(self): .
2021-08-27 18:57:17
245
原创 Python 单入口模式
##包的导入'''文件->模块文件夹->包再文件夹中有一个脚本__init__.py 作用:用来初始化包的脚本'''##1.普通写法'''import package1package1.mylist()'''#2.默认 :通过 文件夹.文件.函数()'''import package1.mypathpackage1.mypath.mylist()'''#3.借助 在初始化脚本文件__init__中引入对应的mypath模块,间接导入mypath##4..
2021-08-27 11:12:49
278
原创 Python 模块的导入
###导入一次,终身受益,重复导入也是只导入一次#part1import mymodule#模块.变量print(mymodule.xboy)#模块.函数mymodule.car()#模块.类mymodule.car().name##part2import mymodule as md#模块.变量print(md.xboy)#模块.函数md.car()#模块.类md.car().name##导入任意路径下的模块import sysprint(sys.path.
2021-08-27 10:00:40
119
原创 Python trafile 模块
#####tarfile 压缩模块 后缀为.tar | .tar.gz | .tar.bz2import tarfile##1.创建tar压缩包#(1)创建压缩包'''.tar压缩包只打包,不压缩'''tf=tarfile.open('ceshi1029.tar','w',encoding='utf-8')##写入文件到压缩包中tf.add('添加的文件路径','别名')tf.add('xxxx','xxxx')##关闭压缩包tf.close()##(2).创建.tar.
2021-08-26 11:12:28
305
原创 Python 常用os模块
os ---系统模块####os模块 --系统进行操作import osos.system('touch ceshi101.txt') ##创建文件os.system('rm -rf ceshi101.txt') ##删除文件os.system('ipconfig')##popen() 执行系统命令返回对象,通过read()方法都读出字符串'''优点:在输入字符串时,可以优先转换成utf-8编码集'''obj=os.po
2021-08-25 18:34:17
818
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人
RSS订阅