2025-09-10:仓库迁移
This commit is contained in:
16
7.函数/9.用函数替代只有单个方法的类.py
Normal file
16
7.函数/9.用函数替代只有单个方法的类.py
Normal file
@@ -0,0 +1,16 @@
|
||||
from urllib.request import urlopen
|
||||
|
||||
class UrlTemplate:
|
||||
def __init__(self, template):
|
||||
self.template = template
|
||||
|
||||
def open(self, **kwargs):
|
||||
return urlopen(self.template.format_map(kwargs))
|
||||
|
||||
# 对于这个类,我们可以使用闭包来进行重写
|
||||
def url_template(template):
|
||||
def opener(**kwargs):
|
||||
|
||||
return urlopen(template.format_map(kwargs))
|
||||
return opener
|
||||
|
Reference in New Issue
Block a user