Scripts are simple programs where the instructions sit in the main program and are not sorted into functions or objects.
Procedural programming is where I encapsulate the instructions into functions (also known in other languages as procedures, hence the term procedural) with particular inputs (arguments) and outputs (returned values).
Object Oriented Programming is like the next step along from using functions, and it bundles the information and instructions into objects, which often represent real things. An object will often have methods (instructions for how the object behaves, basically the same as the functions we have looked at before), and attributes (information about the state of the object, similar to variables but specific to that object).
Classes are the templates that each object of that class will follow. Think of the design for a car, with what it should be capable of and what shape each car should be. The objects would then be all the cars made to that design. They may have different attributes (different number plates, different owners, different colour schemes) but they are all from the same design.
The procedural programming we have done before seems adequate. So why bother with OOP?
Encapsulation: An object does not need to reveal its inner workings to other objects or functions in the rest of the program. Instead it interacts through its methods. Other parts of the program will call on that object's methods and the method may return an answer in a particular format. The class should create objects that are as self-contained as possible, with all the methods and attributes it needs to do its job. This means that debugging can often be narrowed down to a faulty object or class.
Inheritance: A class can have subclasses - variations on a theme. This means that a programmer does not have to create the code for a slightly different class from scratch but can say "Take this class, but change this and this". Think of a design for a car (a class) with hatchback or coupe variants (subclasses). For the objects of these subclasses, many of the components and interfaces are the same (the car designers can reuse much of the original design - inherited from the main class) but with some differences.
Closer modelling of real-life situations: The car analogy is an example. Many programs help users deal with things such as hotels (Trivago), items up for auction (eBay) or fantastic monsters (World of Warcraft). Treating these things as objects and classes makes it easier for the programmers to understand how the program should work.
So how about a quick program to show the basics of objects and classes?
class test:
def __init__(self, num):
self.number = num
self.doublenum = num * 2
self.x = "Hello World"
def show(self):
print("First number is: ", self.number)
print("Second number is: ", self.doublenum)
print("String is: ", self.x)
testobject = test(3)
print ("Attributes")
print (testobject.number)
print (testobject.doublenum)
print (testobject.x)
print("Calling show() function")
testobject.show()
The new part here is at the top, with the keyword class - it tells Python that we are defining a new class, followed by the name (here the class name is test).
There are two methods (class-defined functions) for this particular class (other classes can have many more), __init__ and show.
__init__ is actually a special built-in function that is useful in defining how to create new objects. It always takes at least one argument, self, which refers to the object being created, and in this case one other argument, here an integer called num. Confusingly, you don't need to include self when calling functions in classes (which should be called methods) - it is automatically included in the function call to create the class, but you need to include it in the definition. In __init__ the various attributes are initialised (given values, like variables outside objects). For each attribute initialised, you need to have self. in front. This tells __init__ that this attribute applies to this object being created.
show is just a user-defined method that I wrote to display the attributes of the test class. Again, you need to include self as an argument in the definition, even though it is not needed in the method call. Similar to __init__ you also need to have self. in front of attribute names to tell the program which object these attributes belong to.
testobject = test(3) is the first line outside the class definition (i.e. part of the main program) and it tells the program to create an object using the test class (sometimes called an instance of that class), with an argument of 3 and then assign that object to the variable testobject. This is similar to a function call (with arguments included inside round brackets) but we have defined test as a class, not a function.
The next set of print statements directly access the attributes of the testobject object, a bit like accessing a variable, but you need to state which object the attributes belong to.
Finally testobject.show() is a method call, i.e. it tells Python to run the show() method in the testobject object. In some ways it is similar to a function call, but the method is contained within the object.
And the result?
>>>
RESTART: C:/Users/John/Dropbox/Misc Programming/Python/python3/objecttest.py
Attributes
3
6
Hello World
Calling show() function
First number is: 3
Second number is: 6
String is: Hello World
>>>
>>>
RESTART: C:/Users/John/Dropbox/Misc Programming/Python/python3/objecttest.py
Attributes
3
6
Hello World
Calling show() function
First number is: 3
Second number is: 6
String is: Hello World
>>>
No comments:
Post a Comment