Python 类方法的链式调用
之前在学selenium的时候里面有一个类ActionChains有一种比较奇特的类方法的调用形式:
from selenium.webdriver import ActionChains
from selenium.webdriver import Chrome
from time import sleep
driver=Chrome(r’/Users/jcgg/Desktop/tools/chromedriver’)
driver.get(r’https://www.baidu.com/’)
element=driver.find_element_by_id(“kw”)
ActionChains(driver).move_to_element(element).context_click().perform()
sleep(10)
driver.close()
这边move_to_element,context_click,perform都是类ActionChains的方法,在查看源码后发现最终执行的方法是perform,前面的两个方法只不过是依次往实例对象ActionChains(driver)列表中添加所需要执行的方法而已,然后再由perform方法统一调用,顺序执行。
这边我写了一个简化后能实现类方法的链式调用的类
class Chain(