单继承
class luban:
def __init__(self, name):
self.name = name
self.skill = "摸鱼飞弹"
self.damageLevel = 20
def attack(self):
print("{} 使用了技能{} ,给敌方带来了极大的困扰\n"
"并有{}% 的机会造成一击必杀的效果".format(self.name, self.skill, self.damageLevel))
class luban7(luban):
pass
if __name__ == '__main__':
lu = luban7("鲁班七号")
lu.attack()
多继承
class luban:
def __init__(self, name):
self.name = name
self.skill0 = "摸鱼飞弹"
self.damageLevel = 20
def attack(self):
print("{} 使用了技能{} ,给敌方带来了极大的困扰\n"
"并有{}% 的机会造成一击必杀的效果".format(self.name, self.skill0, self.damageLevel))
class spaceLab:
def __init__(self, name):
self.name = name
self.skill1 = "太空嘴炮"
self.perDamage = 30
def attack(self):
print("{} 使用了技能{} ,给敌方带来了极大的困扰\n"
"并有{}% 的机会摧毁敌方水晶".format(self.name, self.skill1, self.perDamage))
class luban7(luban, spaceLab):
def __init__(self, name):
spaceLab.__init__(self, name)
luban.__init__(self, name)
self.name = name
self.skill2 = "7号嘴炮"
self.damage = 50
def attack(self):
print("{} 使用了技能{} ,给敌方带来了极大的困扰\n"
"并有{}% 的机会摧毁自家水晶".format(self.name, self.skill2, self.damage))
def attack0(self):
luban.attack(self)
def attack1(self):
spaceLab.attack(self)
if __name__ == '__main__':
lu = luban7("鲁班七号")
lu.attack()
lu.attack0()
lu.attack1()
@property
class Demo:
def __init__(self, v):
self.__value = v
@property
def set(self):
return self.__value
@set.setter
def set(self, v):
self.__value = v
if __name__ == '__main__':
obj = Demo(20)
print(obj.set)
obj.set = 30
print(obj.set)
获取目录流
import os
path = "../day2"
if os.path.exists(path):
print("文件夹存在")
else:
print("文件夹不存在")
os.mkdir(path)
files = os.listdir(path)
print(type(files))
print(files)
print("1212")
for file in files:
if os.path.isfile(path + "/" + file):
print(file, "普通文件")
elif os.path.isdir(path + "/" + file):
print(file, "目录文件")
文件的操作
import os
if __name__ == '__main__':
with open(file="./file.txt", mode="a+") as fd:
fd.seek(0, os.SEEK_SET)
buf = fd.readline(5)
print(buf)
fd.close()
print("hello world")
---------------------------------------------------------------------
try:
file = open("file.txt", "w")
file.write("nihaomemedaa\n")
except (IOError, FileNotFoundError) as err:
print("open", err)
exit(-1)
try:
buf = "123456789\n"
file.write(buf)
buf1 = b"hello world\n"
file.write(buf1.decode())
l1 = ["nice\n"]
file.write(l1[0])
l2 = ["I'm groot ", "Spide man ", "Iron man"]
file.writelines(l2)
except (TypeError,) as err:
print("write:", err)
exit(-1)
file.close()
异常
def res(x, y):
if y == 0:
raise ValueError("y == 0")
return x/y
if __name__ == '__main__':
a = int(input("a="))
b = int(input("b="))
try:
print("x / y =", res(a, b))
except (ValueError, ) as err:
print("除数y不能为0", err)
多线程
from time import sleep
from threading import Thread
def music(name):
for i in range(5):
print("I was listening music:{}".format(name))
sleep(1)
def study(name):
for i in range(2):
print("I was study:{}".format(name))
sleep(5)
if __name__ == '__main__':
t1 = Thread(target=music, args=("Always online", ))
t1.start()
t2 = Thread(target=study, args=("《杀死那只知更鸟》", ))
t2.start()
t1.join()
t2.join()
多进程
from multiprocessing import Process
from time import sleep
def music(name):
for i in range(5):
print("I was listening music:{}".format(name))
sleep(1)
def study(name):
for i in range(2):
print("I was study:{}".format(name))
sleep(5)
if __name__ == '__main__':
t1 = Process(target=music, args=("Always online", ))
t1.start()
t2 = Process(target=study, args=("《杀死那只知更鸟》", ))
t2.start()
t1.join()
t2.join()
互斥锁
from time import sleep
from threading import Thread, Lock
def music(name, lock):
with lock:
for i in range(5):
print("I was listening music:{}".format(name))
sleep(1)
def study(name, lock):
with lock:
for i in range(2):
print("I was study:{}".format(name))
sleep(5)
if __name__ == '__main__':
lock = Lock()
t1 = Thread(target=music, args=("Always online", lock))
t1.start()
t2 = Thread(target=study, args=("《杀死那只知更鸟》", lock))
t2.start()
t1.join()
t2.join()
Python进程间通信的方法
管道
消息队列
共享内存
TCP服务器
from socket import *
sockfd = socket(AF_INET, SOCK_STREAM, 0)
sockfd.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
sockfd.bind((INADDR_ANY.to_bytes(0, byteorder="little"), 6666))
sockfd.listen(10)
while True:
connfd, cliaddr = sockfd.accept()
"""
创建线程,传递连接套接字,实现多线程的TCP服务器
"""
print(cliaddr)
while True:
buf = connfd.recv(32).decode()
print("recv:", buf)
if len(buf) == 0:
break
if buf == "exit":
connfd.close()
sockfd.close()
exit(-1)
connfd.close()
TCP客户端
from socket import *
sockfd = socket(AF_INET, SOCK_STREAM, 0)
sockfd.connect(("127.0.0.1", 6666))
while True:
data = input("data=").encode()
sockfd.send(data)