摘要: (1)私有属性 class MyClass: attr1 = 'attr1' # 共有属性 _attr2 = '_attr2' # 私有属性,可直接访问 __attr3 = '__attr3' # 私有属性,改变了属性名:_MyClass__attr3m1 = MyClass()print(m1.a 阅读全文
posted @ 2022-04-22 22:49 狒狒桑 阅读(20) 评论(0) 推荐(0) 编辑
摘要: class Animal: def run(self): print(' animal run ')class Cat(Animal): def run(self): print(' cat run ')class Dog(Animal): def run(self): print(' dog ru 阅读全文
posted @ 2022-04-22 22:39 狒狒桑 阅读(23) 评论(0) 推荐(0) 编辑
摘要: class MyOperator: def __init__(self, data): self.data = data def __add__(self, other): return self.data + other.data def __sub__(self, other): return 阅读全文
posted @ 2022-04-22 21:56 狒狒桑 阅读(23) 评论(0) 推荐(0) 编辑
摘要: (1)文件读取--上下文管理器 with open(filename,'w') as f : class MyOpen: def __init__(self, filename, mode, encoding='utf-8'): self.filename = filename self.mode 阅读全文
posted @ 2022-04-22 21:46 狒狒桑 阅读(21) 评论(0) 推荐(0) 编辑
摘要: class Decorator: def __init__(self, func): self.func = func def __call__(self, *args, **kwargs): print(' call ') return self.func(*args, **kwargs)@Dec 阅读全文
posted @ 2022-04-22 18:58 狒狒桑 阅读(12) 评论(0) 推荐(0) 编辑
摘要: (1)不用装饰器实现 class MyClass2(object): __instance = None def __new__(cls, *args, **kwargs): if not cls.__instance: cls.__instance = object.__new__(cls) re 阅读全文
posted @ 2022-04-22 18:47 狒狒桑 阅读(15) 评论(0) 推荐(0) 编辑
摘要: 类方法和静态方法的区别: (1)类方法可以操作类属性,静态方法不能操作类属性 (2)类方法】静态方法可以用 类.类方法调用;实例方法只能通过实例对象调用 class MyClass: def __init__(self, name): self.name = name # 设置类方法 @classm 阅读全文
posted @ 2022-04-22 17:11 狒狒桑 阅读(43) 评论(0) 推荐(0) 编辑
摘要: (1)装饰器函数--装饰无参函数 def login(func): print("login11111") def fun(): print("login2222") func() return fun@logindef func(): print('func ')# func() (2)装饰器函数 阅读全文
posted @ 2022-04-22 16:31 狒狒桑 阅读(22) 评论(0) 推荐(0) 编辑
摘要: 闭包 代码: def fun(num): def run(): print('num:', num) return runfun(10)() 条件: 1. 函数中嵌套函数2. 外层函数返回内层嵌套函数名3. 内层函数调用外层函数的非全局变量 作用: 实现数据锁定,提高稳定性 阅读全文
posted @ 2022-04-22 15:48 狒狒桑 阅读(3) 评论(0) 推荐(0) 编辑
摘要: (1)filter:过滤函数,根据函数过滤列表 方法一:输出:[1, 3, 5, 3, 7] def fun(n): return n < 10li = [21, 34, 1, 3, 34, 5, 3, 7, 10]res = filter(fun, li)print(list(res)) 方法二: 阅读全文
posted @ 2022-04-22 14:30 狒狒桑 阅读(31) 评论(0) 推荐(0) 编辑
摘要: # 递归求阶乘def fun(n): if n <= 1: return n else: return n * fun(n - 1)print([fun(i) for i in range(10)])# 非递归求阶乘def fun1(n): res = 1 i = 1 if n == 0: retu 阅读全文
posted @ 2022-04-22 13:44 狒狒桑 阅读(31) 评论(0) 推荐(0) 编辑
摘要: # 生成器def gen_func(): for i in range(10): j = yield i print("j=", j)g = gen_func()print(next(g))print(next(g))print(g.send(10))g.close()# g.throw(Value 阅读全文
posted @ 2022-04-22 13:18 狒狒桑 阅读(11) 评论(0) 推荐(0) 编辑