面向对象编程
python-less6
面向对象
class className(父类):
attrName= .... 公有
__priAttr=.....私有
def __init__ (self.....):
.........
def classFun(self.....):
...........
super().fn
多继承
多继承调用同名方法 默认先继承先调
调用父类的方法
super(Class,c).fun()
__init__ : 构造函数,在生成对象时调用
__del__ : 析构函数,释放对象时使用
__repr__ : 打印,转换
__setitem__ : 按照索引赋值
__getitem__: 按照索引获取值
__len__: 获得长度
__cmp__: 比较运算
__call__: 函数调用
__add__: 加运算
__sub__: 减运算
__mul__: 乘运算
__div__: 除运算
__mod__: 求余运算
__pow__: 乘方
Api学习
操作系统api=》os模块 import os
getcwd()返回当前工作目录
chdir(path)修改工作目录为path
system(cmd)执行系统命令 cmd
使用dir(os) help(os) 查看os模块信息
文件操作高级接口
mod shuitl
shuitl.copyfile/move
文件通配符glob
glob.glob(“*.py”)当前目录py文件列表
命令行参数sys
sys.argv
错误输出和重定向
sys.stdin
sys.stderr
sys.stdout
sys.exit
正则表达式支持 re模块
re.findall(r’rep’,str)
re.sub
数学模块 math。提供C函数库访问
随机数模块 random。
random.choice(arr)
random.random()
random.randrange(6)
互联网访问
urlib.request——
smtplib 邮件
处理时间datetime模块
数据压缩zlib ....
性能检测
from timeit import Timer
>>> Timer('t=a; a=b; b=t', 'a=1; b=2').timeit()
相对于 timeit 的细粒度,:mod:profile 和 pstats 模块提供了针对更大代码块的时间度量工具。
测试模块doctest
import doctest
doctest.testmod() # 自动验证嵌入测试
unittest模块 独立的测试文件
编码定义
# -*- coding: cp-1252 -*-
保留字符模块
>>> import keyword
>>> keyword.kwlist
同一个代码块的语句必须包含相同的缩进空格数。
IndentationError: unindent does not match any outer indentation level
多行语句使用\
在 [], {}, 或 () 中的多行语句,不需要使用反斜杠(\)
total = item_one + \
item_two + \
item_three
Python可以在同一行中使用多条语句,语句之间使用分号(;)分割
领取专属 10元无门槛券
私享最新 技术干货