4

Possible Duplicate:
Python ‘self’ explained

I just wrote a code as below with the help of selenium documentation, but confused with one what self does some methods argument list? Why I need to import unittest class?

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()
Community
  • 1
  • 1
CodeLover
  • 1,054
  • 6
  • 24
  • 40
  • The same thing as with any other class. If you're trying to use a third-party library as big as Selenium is without knowing these basics, you're getting way ahead of yourself. – Karl Knechtel Dec 30 '12 at 12:07

1 Answers1

5

self is used to represent the calling instance of a class in case of member methods. This is required so that the member methods of a class act on the correct object. This does not have anything to do with Selenium, but is a general feature of the language.

It is similar to the this argument in C++

When a class is defined, the self argument is used when defining data members of the class as is being done in your class.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
asheeshr
  • 4,088
  • 6
  • 31
  • 50
  • so from where in that body `class` object is being generated? – CodeLover Dec 30 '12 at 10:38
  • There is no class object being generated in the body. self is also used when defining the class. – asheeshr Dec 30 '12 at 10:40
  • @AshRh `if __name__ == "__main__": unittest.main()` what it is doing in the code? could you please explain? – CodeLover Dec 30 '12 at 10:42
  • That has nothing to do with `self` or the class definition you have included in your question. – asheeshr Dec 30 '12 at 10:44
  • 1
    `unittest.main()` is simply calling the main function defined in the unittest module – asheeshr Dec 30 '12 at 10:45
  • what we know for all basic OOP technologies is that without creating any object of a class,we can't invoke the methods written inside that class. But in my above code does the OOP technologies such rules are maintained ? If yes how and from where? – CodeLover Dec 30 '12 at 10:48
  • Yes, they are. Also, Python classes are objects themselves as well. So the rules for objects apply on class definitions as well. – asheeshr Dec 30 '12 at 10:52
  • so in python there is no need to create the object of a class inside the `main()` as we did for `Java` or `C++` using `new` keyword ,right? – CodeLover Dec 30 '12 at 10:55
  • 1
    Selenium reuses the `unittest` framework here, and that framework creates instances of `TestCase` classes as needed to run tests. Creating an instance of `PythonOrgSearch` is taken care off elsewhere. – Martijn Pieters Dec 30 '12 at 10:56
  • 1
    To create a class object, you need to do `object = classname()` in python. Explicit memory allocation is not required anywhere in Python (as best i know). – asheeshr Dec 30 '12 at 11:00
  • 2
    @AshRj: and yes, memory management in Python is taken care of by the python runtime. – Martijn Pieters Dec 30 '12 at 11:05
  • @MartijnPieters Does Python offer *any* low level memory access methods ? – asheeshr Dec 30 '12 at 11:07
  • @MartijnPieters now after the code i written and save it as `Mytest.py`. after that if i run it as python `Mytest.py` will it work? or I need to create a object also at run time for it? – CodeLover Dec 30 '12 at 11:08
  • 1
    @AshRj: you can inspect the [garbage collector](http://docs.python.org/2/library/gc.html) state. That's it. – Martijn Pieters Dec 30 '12 at 11:08
  • 1
    @user1897085: follow the selenium documentation; I'd expect that to work though. – Martijn Pieters Dec 30 '12 at 11:09
  • @MartijnPieters if it works,how the class will be able to work without its object creations? – CodeLover Dec 30 '12 at 11:11
  • @MartijnPieters Thank you again. Perseverance (at this post) did pay off at last. Learned something new :) – asheeshr Dec 30 '12 at 11:13
  • 3
    @user1897085: Why don't you read the [python tutorial](http://docs.python.org/2/tutorial/). – Martijn Pieters Dec 30 '12 at 11:13