随笔分类 -  Python 高级

摘要:__getattr__ 这个魔法函数会在类中查找不到属性时调用 __getattribute__ 阅读全文
posted @ 2019-08-11 17:22 下路派出所 阅读(312) 评论(0) 推荐(0)
摘要:from datetime import date, datetime class User: def __init__(self, name, birthday): self.name = name self.birthday = birthday self._age = 0 @property def age(self... 阅读全文
posted @ 2019-08-11 17:12 下路派出所 阅读(182) 评论(0) 推荐(0)
摘要:1. del是删除对象 2. python中的垃圾回收是删除引用计数 阅读全文
posted @ 2019-08-11 10:41 下路派出所 阅读(482) 评论(0) 推荐(0)
摘要:1. 一旦初始化,并不可以改变 2. 可以作为字典的键值 阅读全文
posted @ 2019-08-11 10:15 下路派出所 阅读(306) 评论(0) 推荐(0)
摘要:from collections.abc import Mapping, MutableMapping#dict属于mapping类型, MutalbelMapping继承Mappinga = {}print(isinstance(a, Mapping))print (isinstance(a, MutableMapping)) 阅读全文
posted @ 2019-08-10 22:50 下路派出所 阅读(307) 评论(0) 推荐(0)
摘要:__getitem__ 单独实现这个魔法函数,可以让这个类成为一个可迭代的对象,并且可以通过使用下标获取类中元素值下标的元素 __iter__ 这个是返回一个可迭代的对象,如果一个类实现了这个魔法函数,那么这个类就是可迭代对象,并且实现了__next__这个魔法函数的话,可以通过for循环遍历;__ 阅读全文
posted @ 2019-08-10 22:33 下路派出所 阅读(2041) 评论(0) 推荐(1)
摘要:import numbers class Group: #支持切片操作 def __init__(self, group_name, company_name, staffs): self.group_name = group_name self.company_name = company_name self.staffs = staffs def __reversed__(self): sel 阅读全文
posted @ 2019-08-10 22:03 下路派出所 阅读(1061) 评论(0) 推荐(0)
摘要:python自省是通过一定的机制对象的内部结构 在python中__dict__与dir()都可以返回一个对象的属性,区别在于: __dict__是对象的一个属性,而dir()是一个built-in的方法; __dict__返回一个对象的属性名和值,即dict类型,而dir()返回一个属性名的lis 阅读全文
posted @ 2019-08-10 14:58 下路派出所 阅读(321) 评论(0) 推荐(0)
摘要:class Date: #构造函数 def __init__(self, year, month, day): self.year = year self.month = month self.day = day def tomorrow(self): self.day += 1 @staticmethod def parse_from_string(date_str): year, month, 阅读全文
posted @ 2019-08-10 14:48 下路派出所 阅读(450) 评论(0) 推荐(0)
摘要:动态语言:不需要去定义变量的类型 协议编程:一个类实现了某个魔法函数,这个类就是什么类型,理解为协议 阅读全文
posted @ 2019-08-10 13:11 下路派出所 阅读(276) 评论(0) 推荐(0)
摘要:调用不同的子类将会产生不同的行为,而无须明确知道这个子类实际上是什么,这是多态的重要应用场景。而在python中,因为鸭子类型(duck typing)使得其多态不是那么酷。鸭子类型是动态类型的一种风格。在这种风格中,一个对象有效的语义,不是由继承自特定的类或实现特定的接口,而是由"当前方法和属性的 阅读全文
posted @ 2019-08-10 13:06 下路派出所 阅读(345) 评论(0) 推荐(0)
摘要:#asyncio 没有提供http协议的接口 aiohttp import asyncio import socket from urllib.parse import urlparse async def get_url(url): #通过socket请求html url = urlparse(url) host = url.netloc path = ur... 阅读全文
posted @ 2019-07-21 19:22 下路派出所 阅读(1700) 评论(0) 推荐(1)
摘要:import asyncio def callback(loop, i): print("success time {} {}".format(i, loop.time())) async def get_html(url): print("start get url") await asyncio.sleep(1) print("end get url") ... 阅读全文
posted @ 2019-07-21 11:23 下路派出所 阅读(3886) 评论(0) 推荐(0)
摘要:#使用多线程:在协程中集成阻塞io import asyncio from concurrent.futures import ThreadPoolExecutor import socket from urllib.parse import urlparse def get_url(url): #通过socket请求html url = urlparse(url) ... 阅读全文
posted @ 2019-07-21 11:16 下路派出所 阅读(2615) 评论(0) 推荐(0)
摘要:1. call_soon, 协程一运行就马上运行 2. call_later 3. call_at 阅读全文
posted @ 2019-07-21 10:55 下路派出所 阅读(2015) 评论(0) 推荐(1)
摘要:import asyncio async def compute(x, y): print("Compute %s + %s ..." % (x, y)) await asyncio.sleep(1.0) return x + y async def print_sum(x, y): result = await compute(x, y) print... 阅读全文
posted @ 2019-07-20 16:50 下路派出所 阅读(1535) 评论(0) 推荐(0)
摘要:import asyncio import time async def get_html(sleep_times): print("waiting") await asyncio.sleep(sleep_times) print("done after {}s".format(sleep_times)) if __name__ == "__main__": ... 阅读全文
posted @ 2019-07-20 16:36 下路派出所 阅读(2593) 评论(0) 推荐(0)
摘要:1. wait, 等待某某执行完成以后才执行下一步 2. gather 比wait更加高级,可以将任务分组,并且取消掉,取消时,必须设置 return_exception为True,不然会抛异常 阅读全文
posted @ 2019-07-20 11:35 下路派出所 阅读(5817) 评论(0) 推荐(2)
摘要:1. 获取协程返回值,实质就是future中的task 2. 使用loop自带的create task, 获取返回值 3. 使用callback,只要await地方的内容一运行完,就会运行callback 使用partial这个模块向callback函数中传入值 阅读全文
posted @ 2019-07-20 10:50 下路派出所 阅读(17625) 评论(1) 推荐(1)
摘要:import asyncio import time async def get_html(url): print("start get url") await asyncio.sleep(2) # 不能使用time.sleep(),这样的话是同步,就不是异步;await就相当于yield from print("end get url") if __name__ ==... 阅读全文
posted @ 2019-07-20 10:32 下路派出所 阅读(2418) 评论(0) 推荐(0)