- 博客(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 72
原创 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 185
原创 Python 装饰器
定义:装饰器用于拓展原来函数功能的一种语法,返回新函数替换旧函数 优点:在不更改原函数的前提下,给函数拓展新的功能 语法:@ #1.装饰器的原型 def kuozhan(func): def newfunc(): print('蓬头垢面') func() print('容光焕发') return newfunc @kuozhan def func(): print('我是一名学生') func() # func=kuozhan(.
2021-08-28 19:37:25 109
原创 Python 魔法函数
###__add__ 魔法函数(与之相关的__radd__反向加法) ''' 触发时机:使用对象进行运算相加的时候自动触发 功能:对象运算 参数:二个对象参数 返回值:运算后的值 ''' class MyClass(): def __init__(self,num): self.num=num #对象在 加号+ 的左侧时,自动触发 def __add__(self, other): ##self 接受的是对象 ##other 接受的是 .
2021-08-28 09:33:51 156
原创 Python 魔法函数 __call__
# ###___call__魔法函数 ''' 触发时机:把对象当作函数调用的时候自动触发 功能:模拟函数化操作 参数:参数不固定,至少一个self参数 返回值:看需求 ''' #(1).基本语法 import math class MyClass(): def __call__(self): print('call方法被触发') obj=MyClass() obj() #(2)洗衣服的过程 class Wash(): def __call__(self): .
2021-08-27 18:57:17 197
原创 Python 单入口模式
##包的导入 ''' 文件->模块 文件夹->包 再文件夹中有一个脚本__init__.py 作用:用来初始化包的脚本 ''' ##1.普通写法 ''' import package1 package1.mylist() ''' #2.默认 :通过 文件夹.文件.函数() ''' import package1.mypath package1.mypath.mylist() ''' #3.借助 在初始化脚本文件__init__中引入对应的mypath模块,间接导入mypath ##4..
2021-08-27 11:12:49 206
原创 Python 模块的导入
###导入一次,终身受益,重复导入也是只导入一次 #part1 import mymodule #模块.变量 print(mymodule.xboy) #模块.函数 mymodule.car() #模块.类 mymodule.car().name ##part2 import mymodule as md #模块.变量 print(md.xboy) #模块.函数 md.car() #模块.类 md.car().name ##导入任意路径下的模块 import sys print(sys.path.
2021-08-27 10:00:40 69
原创 Python trafile 模块
#####tarfile 压缩模块 后缀为.tar | .tar.gz | .tar.bz2 import 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 226
原创 Python 常用os模块
os ---系统模块 ####os模块 --系统进行操作 import os os.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 723
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人