什么"自我"在selenium python代码中做什么?

Cod*_*ver 3 python python-2.7

可能重复:
Python'self'解释

我刚刚在selenium文档的帮助下编写了如下代码,但与self一些方法有什么混淆argument list?为什么我需要导入unittest课程?

import unittest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

class PythonOrgSearch(unittest.TestCase):

    def setUp(self):
        self.driver = webdriver.Firefox()

    def test_search_in_python_org(self):
        driver = self.driver
        driver.get("http://www.python.org")
        self.assertIn("Python", driver.title)
        elem = driver.find_element_by_name("q")
        elem.send_keys("selenium")
        elem.send_keys(Keys.RETURN)
        self.assertIn("Google", driver.title)

    def tearDown(self):
        self.driver.close()

if __name__ == "__main__":
    unittest.main()
Run Code Online (Sandbox Code Playgroud)

ash*_*shr 5

self用于表示成员方法的类的调用实例.这是必需的,以便类的成员方法作用于正确的对象.这与Selenium没有任何关系,但是该语言的一般特征.

它类似于thisC++中的参数

定义类self时,在定义类的数据成员时使用该参数,就像在类中完成一样.

  • @ user1897085:你为什么不读[python tutorial](http://docs.python.org/2/tutorial/). (3认同)
  • @AshRj:是的,Python中的内存管理由python运行时处理. (2认同)