重点 !!!
| 只记录了我感觉需要记得,还有大部分没有记录,希望可以去看网址-------⬇------ |
网址
python回顾
and(&) or (|)
| |
| 2 & 1 将数值化为2进制计算 10 & 01 结果为0 |
| |
| 2 and 1 判断两数值是否为0,0为false,非0为true.当两边都为true时,返回后面数值的值;有零就为0 |
| |
| 3 or 2 值为第一个且不为0的值 ---> 3 |
| 0 or 3 ---> 3 |
| |
| """所有的非空都是True""" |
| '一些面试题可能是 函数and函数 ' --- 记住,所有的非空都为True |
字符串操作
| |
| str[start:end:step] |
| |
| ''' |
| 1.strip() 去掉空白 \n \t 空格 |
| 2.upper() 大写 |
| 3.split() 切割, 默认用空白切割 |
| 4.replace(old,new) 替换 -- 产生一个新的字符串 |
| 5.join() 拼接 |
| 6.count() 计数 |
| 7.find() 查找 从左往右查找,若没有找到返回-1 找到返回字符的位置数值 |
| rfind() 从右往左查找,若没有找到返回-1 |
| 8.index() 索引查找 |
| 9.startswith() 以xxx开头 |
| 10. len() 长度 |
| ''' |
字典
| dict = {key:value} |
| key为不可变类型 |
| |
| |
| dict.setdefault(key,value) |
| |
| |
| 获取该字典的所有key |
| |
| 获取该字典的所有值 |
| |
| 获取该字典的key和value |
| for key,value in dict.items: |
| pass |
驻留机制 --- 节省内存,快速创建对象
深浅拷贝
网址 -----> 很透彻
列表生成式
| 在一个列表生成式中,for前面的if ... else是表达式,而for后面的if是过滤条件,不能带else |
| L1 = ['Hello', 'World', 18, 'Apple', None] |
| L2 = [i.lower() for i in L1 if isinstance(i, str) ] |
| print(L2) |
| if L2 == ['hello', 'world', 'apple']: |
| print('测试通过!') |
| else: |
| print('测试失败!') |
| if写在for前面必须加else,否则报错 |
函数式编程
| def func(*xx,**yy): |
| pass |
| func(*xx,**yy) |
| '*,**':'形参:聚合, 实参:打散' |
闭包
例题非常有用
| '''闭包: |
| 内层函数使用外层函数的变量 |
| 作用: |
| 1. 保护变量. |
| 2. 常驻内存 |
| ''' |
| |
| def func(): |
| return [lambda x: x * i for i in range(4)] |
| a = [m(2) for m in func()] |
| print(a) |
| |
| 拆分: |
| def func(): |
| list1 = [] |
| for i in range(4): |
| def m(x): |
| f = x*i |
| return f |
| list1.append(m) |
| |
| return list1 |
| a = [m(2) for m in func()] |
| '一般情况下,在我们认知当中,如果一个函数结束,函数的内部所有东西都会释放掉,还给内存,局部变量都会消失。但是闭包是一种特殊情况,如果外函数在结束的时候发现有自己的临时变量将来会在内部函数中用到,就把这个临时变量绑定给了内部函数,然后自己再结束。' |
| |
| |
| def func(): |
| return (lambda x: x * i for i in range(4)) |
| a = [m(2) for m in func()] |
| print(a) |
| |
装饰器
| |
| def log(func): |
| def wrapper(*args, **kw): |
| print('call %s():' % func.__name__) |
| return func(*args, **kw) |
| return wrapper |
| @log |
| def now(): |
| print('2015-3-25') |
| |
| |
| |
| |
| def log(text): |
| def decorator(func): |
| def wrapper(*args, **kw): |
| print('%s %s():' % (text, func.__name__)) |
| return func(*args, **kw) |
| return wrapper |
| return decorator |
| @log('execute') |
| def now(): |
| print('2015-3-25') |
| now() |
| 1.先执行log('execute')返回函数decorator ---> now = decorator(now) |
| 2. decorator(now)执行返回wrapper |
| 3.最后执行now()等价于执行wrapper() |
| |
| |
| def add_qx(func): |
| print("---开始进行装饰权限1的功能--") |
| |
| def call_func(*args, **kwargs): |
| print("----这是权限验证1----") |
| return func(*args, **kwargs) |
| |
| return call_func |
| |
| def add_xx(func): |
| print("---开始进行装饰xxx的功能--") |
| |
| def call_func(*args, **kwargs): |
| print("----这是xxx的功能----") |
| return func(*args, **kwargs) |
| |
| return call_func |
| |
| @add_qx |
| def call_func(*args, **kwargs): |
| print("----这是xxx的功能----") |
| return func(*args, **kwargs) |
| |
| @add_xx |
| def test1(): |
| print("-----test1------") |
| |
| test1() |
| |
| 1. test1刚开始被add_xx装饰 --- > test1 = add_xx(test1)执行后返回add_xx中的call_func,func则为test1 |
| 2.返回的call_func进一步被add_qx装饰 ---> call_func = add_qx(call_func) |
| @add_qx |
| def call_func(*args, **kwargs): |
| print("----这是xxx的功能----") |
| return func(*args, **kwargs) |
| 3.装饰后add_qx中的func为add_xx中的call_func |
| 4. 装饰后返回add_qx中的call_func |
| 5.即test1经过多次装饰 ---> test1 = call_func |
| 6.执行test1() --- >等于执行call_func() |
生成器
| 生成器 |
| 写生成器的方式: |
| 1. 生成器函数 -> yield |
| 2. 生成器表达式 -> (结果 for循环 if) |
| 特点: |
| 1. 省内存 |
| 2. 惰性机制(面试题) |
| 3. 只能向前. 不能反复 |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| def triangles(): |
| L =[1] |
| while True: |
| yield L[:] |
| L.append(0) |
| L=[L[i]+L[i-1] for i in range(len(L))] |
| n = 0 |
| results = [] |
| for t in triangles(): |
| results.append(t) |
| n = n + 1 |
| if n == 10: |
| break |
| |
| for t in results: |
| print(t) |
| if results == [ |
| [1], |
| [1, 1], |
| [1, 2, 1], |
| [1, 3, 3, 1], |
| [1, 4, 6, 4, 1], |
| [1, 5, 10, 10, 5, 1], |
| [1, 6, 15, 20, 15, 6, 1], |
| [1, 7, 21, 35, 35, 21, 7, 1], |
| [1, 8, 28, 56, 70, 56, 28, 8, 1], |
| [1, 9, 36, 84, 126, 126, 84, 36, 9, 1] |
| ]: |
| print('测试通过!') |
| else: |
| print('测试失败!') |
高阶函数
filter
| |
| sorted(迭代对象, key = func, reverse = xxx) |
| map(func, iter) 映射 |
| filter(func, iter) 筛选 |
| reduce(func,iter) 将上一次的结果作为参数 |
map
| |
| def f(x): |
| return x * x |
| r = map(f, [1, 2, 3, 4, 5, 6, 7, 8, 9]) |
| list(r) ---> [1, 4, 9, 16, 25, 36, 49, 64, 81] |
reduce
| |
| def prod(L): |
| def f(x, y): |
| return x * y |
| value = reduce(f, L) |
| return value |
| print('3 * 5 * 7 * 9 =', prod([3, 5, 7, 9])) |
| if prod([3, 5, 7, 9]) == 945: |
| print('测试成功!') |
| else: |
| print('测试失败!') |
sorted
| |
| sorted([36, 5, -12, 9, -21], key=abs) |
| [5, 9, -12, -21, 36] |
| |
| L = [('Bob', 75), ('Adam', 92), ('Bart', 66), ('Lisa', 88)] |
| def by_name(t): |
| return t[0] |
| L2 = sorted(L, key=by_name) |
| print(L2) |
内置模块
random
| |
| random.random() |
| random.uniform() |
| |
| random.randint() 随机整数 [m,n] |
| |
| random.choice() 从列表中随机选择一个 |
| random.sample() 从列表中随机选择n个 |
| |
| random.shuffle() 打乱 |
time
| |
| 时间戳 |
| time.time() 当前系统时间, 从1970-01-01 08:00:00 |
| |
| 格式化时间 |
| time.strftime("%y-%m-%d %H:%M:%S") 字符串格式化时间 f: format |
| |
| 结构化时间 |
| time.localtime() 本质是一个元组. 用来做转化的 |
| |
| 时间戳转化成格式化时间 |
| |
| n = 190000000 |
| |
| struct_time = time.localtime(n) |
| |
| |
| format_time = time.strftime('%Y-%m-%d %H:%M:%S',struct_time) |
| print(format_time) |
| |
| |
| fromat_time = input('请输入时间(yyyy-MM-dd HH:mm:ss):') |
| |
| struct_time = time.strptime(fromat_time,"%Y-%m-%d %H:%M:%S") |
| |
| n = time.mktime(struct_time) |
| print(n) |
| |
datetime
| |
| from datetime import datetime |
| now_time = datetime.now() |
| |
| |
| dt = datetime(2015, 4, 19, 12, 20) |
| |
| |
| dt = datetime.now() |
| n = datetime.timestamp(dt) |
| print(n) |
| |
| |
| n = 19000000 |
| format_time = datetime.fromtimestamp(n) |
| print(format_time) |
| |
| |
| format_time = '1970-08-09 05:46:40' |
| date_time = datetime.strptime(format_time,'%Y-%m-%d %H:%M:%S') |
| print(date_time) |
| print(type(date_time)) ----> <class 'datetime.datetime'> |
| |
| |
| now_time = datetime.now() |
| str_time = datetime.strftime(now_time,'%Y-%m-%d %H:%M:%S') |
| print(str_time) |
| print(type(str_time)) ---> <class 'str'> |
| |
| |
| now_time = datetime.now() |
| print(now_time) 2021-07-13 16:45:50.606782 |
| next_time = now_time + timedelta(hours=1) |
| print(next_time) 2021-07-13 17:45:50.606782 |
| |
| |
| 本地时间是指系统设定时区的时间,例如北京时间是UTC+8:00时区的时间,而UTC时间指UTC+0:00时区的时间。 |
| 一个datetime类型有一个时区属性tzinfo,但是默认为None,所以无法区分这个datetime到底是哪个时区,除非强行给datetime设置一个时区: |
| tz_utc_8 = timezone(timedelta(hours=8)) |
| now_time = datetime.now() |
| dt = now_time.replace(tzinfo=tz_utc_8) |
| print(dt) |
| |
| |
| 'utcnow()拿到当前的UTC时间,再转换为任意时区的时间' |
| |
| utc_dt = datetime.utcnow().replace(tzinfo=timezone.utc) |
| |
| |
| bj_dt = utc_dt.astimezone(timezone(timedelta(hours=8))) |
| |
| |
| def to_timestamp(dt_str, tz_str): |
| tz = int(tz_str[3:-3]) |
| print(tz) |
| cday = datetime.strptime(dt_str, '%Y-%m-%d %H:%M:%S') |
| |
| tz_utc = timezone(timedelta(hours=tz)) |
| |
| dt = cday.replace(tzinfo=tz_utc) |
| |
| return dt.timestamp() |
| |
| t1 = to_timestamp('2015-6-1 08:10:30', 'UTC+7:00') |
| assert t1 == 1433121030.0, t1 |
| |
| t2 = to_timestamp('2015-5-31 16:10:30', 'UTC-09:00') |
| assert t2 == 1433121030.0, t2 |
| |
| print('ok') |
os
| os和操作系统相关 |
| 1 os.mkdir() 创建一个文件夹 |
| 2 os.makedirs() 创建一个多级目录 |
| 3 os.rmdir() |
| 4 os.removedirs() |
| |
| os.listdir() 列出目标文件夹内的所有文件 |
| |
| os.path.isfile |
| os.path.isdir |
| os.path.exists |
| |
| os.path.join |
| os.path.getsize() 文件大小 |
collections
| 'collections' : |
| - namedtuple 命名元组 |
| - Counter 计数器 |
| - deque 双向队列 |
| |
| |
| from collections import namedtuple |
| Point = namedtuple("Point", ["x", "y"]) |
| p1 = Point(1,2) |
| print(p1) ---> Point(x=1, y=2) |
| print(p1.x) ---> 1 |
| |
| |
| c = Counter("哈哈哈哈哈哈") |
| print(c) ---> Counter({'哈': 6}) |
| c.update('hello') |
| print(c) ---> Counter({'哈': 6, 'l': 2, 'h': 1, 'e': 1, 'o': 1}) |
hashlib
| |
| |
| |
| md5 = hashlib.md5() |
| md5.update('ashfahsf'.encode('utf8')) |
| print(md5.hexdigest()) |
| '如果数据量过大,可以分批update,加密结果一样' |
| |
| sha1 = hashlib.sha1() |
| sha1.update('how to use sha1 in '.encode('utf-8')) |
| sha1.update('python hashlib?'.encode('utf-8')) |
| print(sha1.hexdigest()) |
| |
| eq: |
| password = '你的密码' |
| salt = '无关字符' |
| md = hashlib.md5() |
| md.update((password+salt).encode('utf8')) |
| md.hexdigest() |
| |
json
| json模块: |
| dumps 数据转化成json |
| loads json转化回数据 |
| |
| dump 序列化之后的bytes写入文件 |
| load 从文件导出bytes. 反序列化成对象 |
| |
| python json |
| None null |
| True true |
| False false |
| dict {} |
| list [] |
| |
| |
| a = {"name": 'wzh', 'age': 18} |
| with open('tset.json','w',encoding='utf8') as f: |
| json.dump(a,f) |
| |
| |
| with open('tset.json','r',encoding='utf8') as f: |
| aa = json.load(f) |
| print(aa) ----> {'name': 'wzh', 'age': 18} |

re
面向对象
--- 封装,继承,多态
私有变量
| class Student(object): |
| def __init__(self,score): |
| self.__score = score |
| |
| s1 = Student(78) |
| |
| |
| |
| def get_score(self): |
| return self.__score |
| |
| |
| |
| def set_score(self,score): |
| self.__score = score |
| s1.set_score(99) |
| |
| |
| s1._Student__score = 99 |
| |
多态 --- 鸭子模型
| def jiao(duck): |
| duck.gagagajiao() |
| |
| class Duck: |
| def gagagajiao(self): |
| print("嘎嘎嘎嘎嘎叫") |
| |
| class Monkey: |
| def gagagajiao(self): |
| print("嘎嘎嘎嘎嘎嘎嘎嘎嘎嘎嘎嘎嘎叫") |
| |
| jiao(Monkey()) |
| |
| '鸭子模型:不关注对象本身,只关注对象行为' |
| '在这个实例中,在本身是一个Monkey,在另一个方面它是Duck,因为它会嘎嘎叫。在不同的视角中,它有不同的身份,这就是多态' |
继承
| class F(object): |
| name = 'wzh' |
| def attack(self): |
| print('阿迪斯') |
| |
| class S(F): |
| def a(self): |
| super().attack() |
| s = S() |
| s.a() |
| print(s.name) |
获取对象信息
反射
| |
| |
| class MyObject(object): |
| def __init__(self): |
| self.x = 9 |
| |
| def power(self): |
| return self.x * self.x |
| |
| |
| my = MyObject() |
| print(hasattr(my,'x')) True |
| print(getattr(my,'x')) 9 |
| print(getattr(my,'z','404')) 获取属性z,若不存在返回404 |
| print(setattr(my,'y',6)) 设置一个属性y,并赋值6 |
| |
| print(hasattr(my,'power')) True |
| print(getattr(my,'power')) <bound method MyObject.power of <__main__.MyObject object at 0x000002A4DF5020C8>> |
| |
实例属性和类属性
| class Student(object): |
| name = '张三' |
| stu = student() |
| print(Student.name) '张三' |
| print(stu.name) '张三' ----> 类属性可以被实例直接获取 |
| |
| stu.name = '李四' |
| Student.name ---> '张三' |
| |
| |
| 实例属性属于各个实例所有,互不干扰; |
| |
| 类属性属于类所有,所有实例共享一个属性; |
| |
| 不要对实例属性和类属性使用相同的名字,否则将产生难以发现的错误。 |
| |
面对对象高级编程
—solts—
| |
| from types import MethodType |
| class Student(object): |
| pass |
| |
| def set_name(self, name): |
| self.name = name |
| |
| |
| s1 = Student() |
| s2 = Student() |
| s3 = Student() |
| |
| s1.set_name = MethodType(set_name, s1) |
| s2.set_name = MethodType(set_name, s2) |
| |
| s1.set_name('s1') |
| s2.set_name('s2') |
| print(hasattr(s1,'name')) |
| print(hasattr(s1,'set_name')) |
| print(getattr(s1,'name')) |
| |
| print(hasattr(s2,'name')) |
| print(getattr(s2,'name')) |
| |
| print(hasattr(Student,'name')) |
| print(hasattr(Student,'set_name')) |
| |
| --------分割线----------- |
| |
| s1 = Student() |
| s2 = Student() |
| s3 = Student() |
| |
| Student.set_name = MethodType(set_name, Student) |
| s1.set_name('s1') |
| s2.set_name('s2') |
| print(hasattr(s1, 'name')) |
| print(getattr(s1, 'name')) |
| print(hasattr(Student, 'name')) |
| print(hasattr(Student, 'set_name')) |
| print(getattr(Student, 'name')) |
| |
| |
| class Student(object): |
| __slots__ = ('name', 'age') |
| |
| stu = Student() |
| stu.name = '张三' |
| stu.score = 99 |
| |
| |
| from types import MethodType |
| class Student(object): |
| def __init__(self, *args, **kwargs): |
| self.name = kwargs.pop('name') |
| self.age = kwargs.pop('age') |
| self.score = kwargs.pop('score') |
| |
| def set_score(self, value): |
| print(self) |
| self.score = value |
| |
| def get_score(self): |
| print(self) |
| return self.score |
| |
| Student.set_score = MethodType(set_score, Student) |
| Student.get_score = MethodType(get_score, Student) |
| t = Student(name="Bryan", age=24, score=80) |
| |
| print(t.score) |
| t.set_score(100) |
| print(t.get_score()) |
| print(t.score) |
@property
| '在方法上面@property可以将方法变为属性,访问直接 实例.属性 就可以访问' |
| '@属性.setter由@property衍生出来,可以设置属性值' |
| class Screen(object): |
| def __init__(self): |
| self._width = 0 |
| self._height = 0 |
| |
| @property |
| def width(self): |
| return self._width |
| |
| @property |
| def height(self): |
| return self._height |
| |
| @width.setter |
| def width(self,value): |
| if not isinstance(value,int): |
| raise ValueError('score must be an integer!') |
| if value<= 0: |
| raise ValueError('int must "大于零"') |
| self._width = value |
| |
| @height.setter |
| def height(self,value): |
| if not isinstance(value,int): |
| raise ValueError('score must be an integer!') |
| if value<= 0: |
| raise ValueError('int must "大于零"') |
| self._height= value |
| |
| @property |
| def resolution(self): |
| return self._height*self._width |
| |
| s = Screen() |
| s.width = 1024 |
| s.height = 768 |
| print('resolution =', s.resolution) |
| if s.resolution == 786432: |
| print('测试通过!') |
| else: |
| print('测试失败!') |
多重继承
| class A(object): |
| def foo(self): |
| print('A foo') |
| def bar(self): |
| print('A bar') |
| |
| class B(object): |
| def foo(self): |
| print('B foo') |
| def bar(self): |
| print('B bar') |
| |
| class C1(A,B): |
| pass |
| |
| class C2(A,B): |
| def bar(self): |
| print('C2-bar') |
| |
| class D(C1,C2): |
| pass |
| |
| if __name__ == '__main__': |
| print(D.__mro__) |
| d=D() |
| d.foo() |
| d.bar() |

再来一道例题:
| class A(object): |
| def foo(self): |
| print('A foo') |
| def bar(self): |
| print('A bar') |
| |
| class B(object): |
| def foo(self): |
| print('B foo') |
| def bar(self): |
| print('B bar') |
| |
| class C1(A): |
| pass |
| |
| class C2(B): |
| def bar(self): |
| print('C2-bar') |
| |
| class D(C1,C2): |
| pass |
| |
| if __name__ == '__main__': |
| print(D.__mro__) |
| d=D() |
| d.foo() |
| d.bar() |

定制类
—str—
| class Student(object): |
| def __init__(self, name): |
| self.name = name |
| |
| def __str__(self): |
| return 'Student object (name: %s)' % self.name |
| |
| print(Student('张三')) |
—repr—
| 在交互式模式下可以看出: |
| stu = Student('李四') |
| stu |
| <__main__.Student object at 0x000001C830E61808> |
| '这是因为直接显示变量调用的不是__str__(),而是__repr__(),两者的区别是__str__()返回用户看到的字符串,而__repr__()返回程序开发者看到的字符串,也就是说,__repr__()是为调试服务的。' |
| |
| |
| '解决办法是再定义一个__repr__()。但是通常__str__()和__repr__()代码都是一样的,所以,有个偷懒的写法' |
| class Student(object): |
| def __init__(self, name): |
| self.name = name |
| def __str__(self): |
| return 'Student object (name=%s)' % self.name |
| __repr__ = __str__ |

—iter—
| '如果一个类想被用于for ... in循环,类似list或tuple那样,就必须实现一个__iter__()方法,该方法返回一个迭代对象,然后,Python的for循环就会不断调用该迭代对象的__next__()方法拿到循环的下一个值,直到遇到StopIteration错误时退出循环' |
| |
| class Fib(object): |
| def __init__(self): |
| self.a, self.b = 0, 1 |
| |
| def __iter__(self): |
| return self |
| |
| def __next__(self): |
| self.a, self.b = self.b, self.a + self.b |
| if self.a > 100000: |
| raise StopIteration() |
| return self.a |
| for n in Fib(): |
| print(n) |
—getitem—
| '定义 : __getitem__(self, key)' |
| |
| 1. |
| |
| class Fib(object): |
| def __getitem__(self, n): |
| a, b = 1, 1 |
| for x in range(n): |
| a, b = b, a + b |
| return a |
| f = Fib() |
| f[10] ---> 89 |
| |
| 2. |
| class Animal: |
| def __init__(self, animal_list): |
| self.animals_name = animal_list |
| |
| def __getitem__(self, index): |
| return self.animals_name[index] |
| |
| animals = Animal(["dog","cat","fish"]) |
| for animal in animals: |
| print(animal) |
| |
—getattr—


| '如果需要避免产生此类错误,我们可以定义__getattr__()' |
| class Student(object): |
| def __init__(self): |
| self.name = 'Michael' |
| |
| def __getattr__(self, attr): |
| if attr=='score': |
| return 99 |
| stu = Student() |
| |
| print(stu.score) --> 99 |
| '注意,只有在没有找到属性的情况下,才调用__getattr__,已有的属性,比如name,不会在__getattr__中查找' |
| |
| '自己如果重新看的话,自己慢慢看,很简单的,当时写了流程图,忘了沾上来...造孽啊' |
| class Chain(object): |
| |
| def __init__(self, path=''): |
| self._path = path |
| |
| def __getattr__(self, path): |
| return Chain('%s/%s' % (self._path, path)) |
| |
| def __str__(self): |
| return self._path |
| |
| __repr__ = __str__ |
| |
| >>> Chain().status.user.timeline.list |
| '/status/user/timeline/list' |
—call—
| '该方法可以调用实例对象本身' |
| class Student(object): |
| def __init__(self, name): |
| self.name = name |
| |
| def __call__(self): |
| print('My name is %s.' % self.name) |
| stu = Student('张三') |
| stu() ---> 会主动调用__call__() |
| |
| |
| callable(对象) |
| |
| int(),len(),str(),list()这些可被直接调用 |

元类
| '基本用不上' |
| 如果需要理解,对照廖雪峰的网址,然后再结合自己元类的代码 D:\python项目\元类和rom |
| 结合这两个可以比较透彻的理解一下 |
ORM(Object Relational Mapping) --->对象关系映射
| 这个比较有用处了,为django后面的ORM操作打基础,可以大概知道ORM怎么来的 |
| D:\python项目\元类和rom |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix