class MyClass(FinderClass):
def _find(self, search, attribute):
if attribute == 'ip':
locator = "some_locator"
if attribute == 'name':
locator = "other_locator"
return self.finder.find(search, locator)
def find_by_ip(self, ip):
return self._find(ip, 'ip')
我可以使用functools'重写上面的例子吗?部分?如果可能的话我想拥有:参数ip应该命名为ip而不是搜索,函数应该在类中创建。
我想得到的是与此类似的东西:
class MyClass(FinderClass):
def _find(self, search, attribute):
if attribute == 'ip':
locator = "some_locator"
if attribute == 'name':
locator = "other_locator"
return self.finder.find(search, locator)
find_by_ip = functools.partial(_find, attribute='ip')
这样可行,但你必须这样称呼它并不酷:
mc = MyClass()
mc.find_by_ip(mc, '127.0.0.1')
该函数的参数也称为search
而不是ip
,但这有点好。