作者jamesxxx1997 (黄~)
看板Python
标题[问题] 关於class的继承在__init__()中的疑惑
时间Tue Dec 26 17:32:38 2017
大家好,我在一个爬虫framework中,建立了两个.py档案
第一个档案中有一个class SeleniumDriver
第二个档案中有一个class LoginPage
LoginPage继承SeleniumDriver
在SeleniumDriver中
def __init__(self,driver):
self.driver = driver
在LoginPage中
def __init__(self,driver):
super().init__(driver)
self.driver = driver
在SeleniumDriver中,有很多使用self.driver的function
ex. self.driver.find_element()
如果我没有在LoginPage的__init__()中用super()
,会调用不到SeleniumDriver中的方法吗? 为什麽呢?
谢谢
PS.
code
class SeleniumDriver ==>
https://goo.gl/A3n4jR
class LoginPage ==>
https://goo.gl/NJFw1N
selenium老师的解释(看不懂) : We are using it to initialize the driver instance in the parent class│
If the parent class does not know about the driver instance, you will be able
to call the methods from the parent class but they will give an exception
because they do not know about driver methods│
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 223.141.87.71
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/Python/M.1514280761.A.1DB.html
1F:→ s860134: __init__你下面那个打错了 12/26 21:15
2F:→ s860134: 另外你都看到 SeleniumDriver 没做啥事情,不去引用没差 12/26 21:15
3F:→ s860134: 你会在意的是哪天 SeleniumDriver __init__ 做了甚麽事 12/26 21:16
4F:→ s860134: LoginPage 是不是要一起改 12/26 21:16
5F:→ jamesxxx1997: 所以其实根本就不用加上super()XD 12/26 22:20
6F:→ jamesxxx1997: 谢谢S大大 12/26 22:21
7F:→ uranusjr: 我的吗啊这用继承写是老师教你的吗 12/26 23:55
8F:→ jamesxxx1997: u大,对的 12/27 00:37
9F:→ jamesxxx1997: 这是一个selenium webdriver的framework 12/27 00:38