17 lines
397 B
Python
17 lines
397 B
Python
|
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
|
||
|
|