[問題] 關於class的繼承在__init__()中的疑惑

看板Python作者 (黃~)時間8年前 (2017/12/26 17:32), 編輯推噓0(009)
留言9則, 3人參與, 8年前最新討論串1/1
大家好,我在一個爬蟲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://www.ptt.cc/bbs/Python/M.1514280761.A.1DB.html

12/26 21:15, 8年前 , 1F
__init__你下面那個打錯了
12/26 21:15, 1F

12/26 21:15, 8年前 , 2F
另外你都看到 SeleniumDriver 沒做啥事情,不去引用沒差
12/26 21:15, 2F

12/26 21:16, 8年前 , 3F
你會在意的是哪天 SeleniumDriver __init__ 做了甚麼事
12/26 21:16, 3F

12/26 21:16, 8年前 , 4F
LoginPage 是不是要一起改
12/26 21:16, 4F

12/26 22:20, 8年前 , 5F
所以其實根本就不用加上super()XD
12/26 22:20, 5F

12/26 22:21, 8年前 , 6F
謝謝S大大
12/26 22:21, 6F

12/26 23:55, 8年前 , 7F
我的嗎啊這用繼承寫是老師教你的嗎
12/26 23:55, 7F

12/27 00:37, 8年前 , 8F
u大,對的
12/27 00:37, 8F

12/27 00:38, 8年前 , 9F
這是一個selenium webdriver的framework
12/27 00:38, 9F
文章代碼(AID): #1QGXSv7R (Python)