摘要: 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) 编辑
摘要: 通过将属性和方法前加__变为私有权限,私有的属性和方法不能直接获取,只能通过类内部获取 class Dog(): def __init__(self): # 私有属性 self.__tooth = 22 self.age = 5 # 更改私有属性 def set_tooth(self): self. 阅读全文
posted @ 2021-03-09 10:58 code-G 阅读(118) 评论(0) 推荐(0) 编辑
摘要: Dog和Cat都继承Animal,都使用父类的init方法,但是greet方法却是不同的,子类对父类进行了覆盖,还编写了自己的run方法 class Animal(object): def __init__(self,name): self.name = name def greet(self): 阅读全文
posted @ 2021-03-09 10:39 code-G 阅读(71) 评论(0) 推荐(0) 编辑
摘要: 单继承 # Master继承Object class Master(object): # __init__是个魔法方法,用于初始化 def __init__(self): self.kongfu = '师父功夫' def print_info(self): print(f'{self.kongfu} 阅读全文
posted @ 2021-03-09 10:33 code-G 阅读(81) 评论(0) 推荐(0) 编辑