views:

282

answers:

4

I am creating an interface for Python scripting. Later I will be dong Python scripting also for automated testing. Is it necessary the at i must use class in my code.Now I have created the code with dictionaries,lists,functions,global and local variables.

Is class necessary?

Help me in this.

+10  A: 

No, of course class is not a must. As Python is a scripting language, you can simply code your scripts without defining your own classes. Classes are useful if you implement a more complex program which needs a structured approach and OOP benfits (encapsulation, polimorphism) help you in doing it.

+1  A: 

It's not needed to make it work, but I would argue that it will become messy to maintain if you do not encapsulate certain things in classes. Classes are something that schould help the programmer to organizes his/her code, not just nice to have fluff.

André
+1  A: 

No you don't need to use classes for scripting.

However, when you start using the unit testing framework unittest, that will involve classes so you need to understand at least how to sub-class the TestCase class, eg:

import unittest
import os

class TestLint(unittest.TestCase):

    def testLintCreatesLog(self):
        # stuff that does things to create the file lint.log removed...
        assert os.path.exists('lint.log')  # this should be here after lint        
        assert os.path.getsize('lint.log') == 0 # nothing in the log - assume happy

if __name__ == '__main__':
    # When this module is executed from the command-line, run all its tests
    unittest.main()
Andy Dent
On the other hand, the excellent nose test framework doesn't require subclassing, nor does py.test.nose is pretty much the best thing out there for unit testing on Python, IMHO.
Tim Lesher
A: 

not necessary since python is not a purely object oriented language but certain things are better written in classes (encapsulation).it becomes easier to build a large project using classes

appusajeev