***面向对象的好处***
更容易扩展、提高代码使用效率,使你的代码组织性更强,更清晰
更适合复杂项目的开发 封装
把功能的实现细节封装起来,只暴露调用接口
继承 多态
接口继承 定义
类----》模板
对象---》实例化的模板 属性
私有属性 __private
公有属性 存在类的内存里,所有*共享
成员属性 ---》实例变量
方法 ---》函数
构造函数
析构函数:实例销毁时,自动执行 静态方法
类方法
属性方法
class Flight(object):
'''我是类的注释'''
def __init__(self, name):
self.flight_name = name def checking_status(self):
print("checking flight %s status " % self.flight_name)
return 1
@property
def flight_status(self):
status = self.checking_status()
if status == 0:
print("flight got canceled...")
elif status == 1:
print("flight is arrived...")
elif status == 2:
print("flight has departured already...")
else:
print("cannot confirm the flight status...,please check later") @flight_status.setter # 修改
def flight_status(self, status):
status_dic = {
0: "canceled",
1: "arrived",
2: "departured"
}
print("\033[31;1mHas changed the flight status to \033[0m", status_dic.get(status)) def __call__(self, *args, **kwargs):
print('--call:',args,kwargs) @flight_status.deleter # 删除
def flight_status(self):
print("status got removed...") f = Flight("CA980")
# f.flight_status
# f.flight_status = 2 # 触发@flight_status.setter
# del f.flight_status # 触发@flight_status.deleter
f('','')
print(f.__doc__)
print(f.__module__)
print(f.__class__)
***异常处理***
一、基本异常处理结构
try:
代码块
except Exception as e:
将错误信息输出写入日志文件 二、复杂结构
try:
。。。
except:
。。。
else:
。。。
finally
。。。
三、异常对象
try:
代码块
except Exception as e:#python内部将错误信息封装到e对象中
将错误信息输出写入日志文件
四、 异常种类
Exception,能将所有的异常都捕获
。。。
其他的处理方法,只能处理某一情况
try:
代码块
except Exception as e:#python内部将错误信息封装到e对象中
将错误信息输出写入日志文件 try:
int('asdd')
list = [1,2]
list[1]
except IndexError as e:
pass
except Exception as e:
pass
else:
print('else')
finally:
print('finally')
======>Exception,其他错误的关系
继承关系
五、主动触发异常 六、断言
assert条件 七、自定义异常 反射
hashattr(容器,名称)#
app.py
import os
import sys BASE_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(BASE_DIR)
from controller import account action = input('>>')
if hasattr(account,action):
func = getattr(account,action)
result =func()
else:
result = ''
print(result)
account.py
def login():
return '请输入永户名密码'
def logout():
return '跳转回登陆界面'
***socket***
server.py
import socket
import os
import sys
server = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
server.bind(('0.0.0.0',8000))
server.listen(5)
print('start listen')
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(BASE_DIR)
new_size = 0
while True:
conn,client_addr = server.accept()
print(conn,client_addr)
num = 0
while True:
try:
data = conn.recv(1024)
if num == 0:
filename = data.decode()
if num == 1:
total_size = int(data.decode())
if num >= 2:
print(filename,'这什么情况')
with open(filename,'ab') as f:
new_size = os.path.getsize(r'%s' % filename)
f.write(data)
print('recv from client:',filename)
conn.send(b'got your message')
num += 1
except Exception as e:
break
client.py
import socket
import os
import sys
import re
# BASE_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)))
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(BASE_DIR)
print(sys.path)
print(BASE_DIR)
client = socket.socket()
client.connect(('localhost',8000))
while True:
input_str = input('[put filename]>>').strip()
input_list = re.split(' ',input_str)
msg = input_list[1]
if input_list[0] != 'put':
print('command error:',input_list[0],'input [put filename] command')
continue
if r'\\' not in msg:
if os.path.exists(msg):
filename = msg
else:
print('not found file')
continue
else:
filename = msg
length_file = os.path.getsize(r'C:\Users\heshaochuan\PycharmProjects\py_s15\day8\反射\test.py')
length_file_str = '%s'%length_file
if len(filename) == 0:continue
client.send(filename.encode())
print('send',filename)
data = client.recv(1024)
if data:
client.send(length_file_str.encode())
data1 = client.recv(1024)
if data1:
with open(filename, 'rb') as f:
for i in f:
client.send(i)
data2 = client.recv(1024)
print('recv from server:',data2)