随笔分类 - python备忘
摘要:import time res = [(time.sleep(1),print(i)) for i in range(10)] print(res) ''' [(None, None), (None, None), (None, None), (None, None), (None, None),
阅读全文
摘要:列表拆分成小列表 l = [i for i in range(16)] n = 3 #大列表中几个数据组成一个小列表 print([l[i:i + n] for i in range(0, len(l), n)]) print([i for i in range(0, len(l), n)]) 使用
阅读全文
摘要:@classmethod class method是和类绑定的方法,不是和类的对象(实例)绑定的方法 class method能够访问类的状态,因为它可以接受一个指向类的参数(cls),而不是指向类实例的参数(self)。 class method可以修改类的状态,并应用到所有的类实例上。 clas
阅读全文
摘要:getattr class ObjectDict(dict): def __init__(self, *args, **kwargs): super(ObjectDict, self).__init__(*args, **kwargs) def __getattr__(self, name): va
阅读全文
摘要:bool,len__和bool相关,在if中会被调用,优先调用__bool,没有就调用__len__ class Test: def __bool__(self): print('__bool__') return False def __len__(self): print('__len__')
阅读全文
摘要:def 函数定义不会执行里面的语句 class 类的定义会执行里面的语句 def test_func(): print("test_func") # 不会执行 class setter(object): def __init__(self, method): self.method = method
阅读全文
摘要:分类 在变量、方法命名中有下列几种情况: 0. xx 公有变量/方法 1. _xx 前置单下划线 2. __xx 前置双下划线 3. __xx__ 前后双下划线 4. xx_ 后置单下划线 结论 1. _名 的变量、函数、类在使用 from xxx import * 时都不会被导入。 2. __名字
阅读全文
摘要:list,tuple,str都是有序序列 https://blog.csdn.net/Alice_lanniste/article/details/51606452
阅读全文
摘要:https://www.jianshu.com/p/9d232e4a3c28 https://www.cnblogs.com/jhao/p/7610218.html https://www.bilibili.com/video/BV1YK4y1875p?p=3
阅读全文
摘要:闭包:一个持有外部环境变量的函数就是闭包;闭包是一个能够访问其他函数作用域的函数 ''' 简化写法 ''' def multipliers(): return [lambda x: i * x for i in range(4)] print([m(2) for m in multipliers()
阅读全文
摘要:python解析XML,HTML # 1 from lxml import etree # 2 import xml.dom.minidom # 3 bs4 # 4 import xmltodict python读取文件共享 ''' https://pypi.org/project/smbproto
阅读全文
摘要:iter 和 next # 首先获得Iterator对象: it = iter([1, 2, 3, 4, 5]) # 循环: while True: try: # 获得下一个值: x = next(it) print(x) except StopIteration: # 遇到StopIteratio
阅读全文
摘要:_getattr_(self, item): 在访问对象的item属性的时候,如果对象并没有这个相应的属性,方法,那么将会调用这个方法来处理。 假如一个对象一个属性:cat.name = "tom",那么在访问cat.name的时候因为当前对象有这个属性,那么将不会调用__getattr__()方法
阅读全文
浙公网安备 33010602011771号