摘要: with语句 # with语句简化文件操作,出错也会关闭文件 with open('abc.txt','r',encoding='utf-8') as file: file_data = file.read() print(file_data) 自写文件管理器 class File(object): 阅读全文
posted @ 2021-03-09 20:50 code-G 阅读(56) 评论(0) 推荐(0) 编辑
摘要: property的装饰器方式使用 class Person(object): def __init__(self): self.__age = 0 @property # 把age方法当属性使用,获取age属性时会调用下面的方法 def age(self): print('获取年龄属性') retu 阅读全文
posted @ 2021-03-09 19:34 code-G 阅读(59) 评论(0) 推荐(0) 编辑
摘要: class person(): # 初始化要装饰的函数 def __init__(self,fn): self.__fn = fn # 在call方法中使用目标函数 def __call__(self, *args, **kwargs): print('呵呵') self.__fn() @perso 阅读全文
posted @ 2021-03-09 19:24 code-G 阅读(69) 评论(0) 推荐(0) 编辑
摘要: 装饰器本质上就是一个闭包函数,装饰器有且只有一个参数,参数为要装饰的函数 作用:不改变原函数,而增强其功能 def decorator(fn): def inner(): print('已登录') fn() return inner @decorator def work(): print('可以进 阅读全文
posted @ 2021-03-09 19:17 code-G 阅读(82) 评论(0) 推荐(0) 编辑
摘要: 闭包的形成条件: 函数嵌套 内部函数使用外部函数的变量或者参数 外部函数返回内部函数。这个使用外部函数变量的内部函数称为闭包 按照容易理解的意思分析一下 func_out()的返回值是func_inner函数,所以func指代的就是func_inner函数 调用func函数,实际上调用的是func_ 阅读全文
posted @ 2021-03-09 18:54 code-G 阅读(65) 评论(0) 推荐(0) 编辑
摘要: 死锁:两个或两个以上的进程(线程)在执行过程中,由于竞争资源或者由于彼此通信而造成的一种阻塞的现象,程序无法推进 from threading import * lock = Lock() def get_value(index): lock.acquire() list1 = [1,2,3,4] 阅读全文
posted @ 2021-03-09 18:28 code-G 阅读(191) 评论(0) 推荐(0) 编辑
摘要: 想知道一些原理,建议去学操作系统 from threading import * num = 0 # 创建锁对象 lock = Lock() def task1(): # 上锁 lock.acquire() global num for i in range(1000000): num += 1 p 阅读全文
posted @ 2021-03-09 18:21 code-G 阅读(83) 评论(0) 推荐(0) 编辑
摘要: 进程是由线程组成的 进程之间全局变量不共享 线程之间资源共享 进程和线程的执行是无序的 # 进程和线程的执行是无序的 from multiprocessing import * from time import * from threading import * def print_info(): 阅读全文
posted @ 2021-03-09 18:16 code-G 阅读(97) 评论(0) 推荐(0) 编辑
摘要: 全局变量共享 from threading import * from time import * list1 = [] def add_data(): for i in range(3): list1.append(i) print('add',list1) sleep(0.2) def read 阅读全文
posted @ 2021-03-09 18:03 code-G 阅读(441) 评论(0) 推荐(0) 编辑
摘要: 主线程等待子线程执行完再结束 # 导包 from threading import * from time import * def dance(): print(current_thread()) for i in range(6): print('跳舞...') sleep(0.2) if __ 阅读全文
posted @ 2021-03-09 17:56 code-G 阅读(1129) 评论(0) 推荐(0) 编辑
摘要: # 导包 from threading import * from time import * def dance(): print(current_thread()) for i in range(3): print('跳舞...') sleep(0.2) def sing(): print(cu 阅读全文
posted @ 2021-03-09 17:52 code-G 阅读(115) 评论(0) 推荐(0) 编辑
摘要: 主进程或等待子进程执行完 # 输出over后主进程内容已经执行完了,但是会等待子进程执行完 from multiprocessing import * from time import * def print_info(): for i in range(10): print(i) sleep(0. 阅读全文
posted @ 2021-03-09 17:47 code-G 阅读(1084) 评论(0) 推荐(0) 编辑
摘要: from multiprocessing import * list1 = [] def add_data(): for i in range(3): list1.append(i) print('add ', list1) def read_data(): print('read ', list1 阅读全文
posted @ 2021-03-09 17:34 code-G 阅读(258) 评论(0) 推荐(0) 编辑
摘要: Python一般使用*arg表示不定长参数,**kwargs表示关键字参数也是不定长的 from multiprocessing import * def print_info(name,age): print(f'{name}, {age}') # args 元组传入 位置参数 # sub_pro 阅读全文
posted @ 2021-03-09 17:31 code-G 阅读(233) 评论(0) 推荐(0) 编辑
摘要: 使用os模块获取进程编号 # 导包 import multiprocessing import time import os def dance(): print('跳舞子进程编号', os.getpid()) print('跳舞的父进程编号', os.getppid()) for i in ran 阅读全文
posted @ 2021-03-09 17:27 code-G 阅读(822) 评论(0) 推荐(0) 编辑
摘要: 顺序执行变为异步执行 # 导包 import multiprocessing import time def dance(): for i in range(3): print('跳舞中...') time.sleep(0.2) def sing(): for i in range(3): prin 阅读全文
posted @ 2021-03-09 17:22 code-G 阅读(57) 评论(0) 推荐(0) 编辑
摘要: 模块实际上就是一个py文件 直接导入 导入指定功能 导入所有功能 # 直接导入模块 import math print(math.sqrt(9)) # 导入模块的功能 此时不用加前缀 from math import sqrt print(sqrt(9)) # 导入模块的所有功能 from math 阅读全文
posted @ 2021-03-09 16:46 code-G 阅读(102) 评论(0) 推荐(0) 编辑
摘要: 自定义异常类 抛出异常 捕获异常 # 继承异常类Exception class ShortPassword(Exception): def __init__(self,length,min_length): self.length = length self.min_length = min_len 阅读全文
posted @ 2021-03-09 16:30 code-G 阅读(116) 评论(0) 推荐(0) 编辑
摘要: 异常格式 ''' 格式: try: 可能发生异常的代码 except: 如果出现异常执行的代码 ''' try: file = open('a.txt','r') except: file = open('a.txt','w') file.close() 捕获指定异常或指定多个异常 # 如果指定的异 阅读全文
posted @ 2021-03-09 16:16 code-G 阅读(88) 评论(0) 推荐(0) 编辑
摘要: init # 魔法方法是指__xx__ 的方法 具有特殊功能 # init 魔法方法初始化。会自动调用 class Washer(): def __init__(self): self.width = 400 self.height = 500 def print_info(self): print 阅读全文
posted @ 2021-03-09 11:17 code-G 阅读(45) 评论(0) 推荐(0) 编辑