In the argument between R and Python, the fact the Python is a full blown Object Oriented Programming(OOP) language gives it a solid advantage to me. Why? OOP gives you the ability to create and use objects in your programs.
Now I remember my first OOP class was in Java. It was a college course and started with reading 4 dense chapters on what made a language OOP. It talked about Objects and methods and data encapsulation. In the end, I just had a headache, and I really didn’t understand anything until I built my first Object.
So, let’s just jump right in and build our first object.
The first step in building an object is to build a class. Now I am going to move quick here, and then work my way back to better explain what we are doing.
So, I am building a 2 function calculator here.
- class myCalc: – First I create my class and name it
- def __init__(self): — this is called a constructor – you need to have one, but you don’t need to use it. Notice I have the word pass on the next line. That means just move on without doing anything. Don’t worry constructors for now. We will cover constructors in a later lesson.
- def myAdd(self, x,y): this is basically a function inside my class, but inside a class we call functions methods. The first argument (self) is a reference to our constructor. Again, don’t worry about it yet, just know it has to be there or you will get an error
- def mySub(self,x,y): Same as above
Create our Object
By assigning our class to a variable name, what we doing is creating an Instance
Now that I created and Instance can call my Methods
Notice when I call myAdd and mySub, I only provide 2 arguments. Like I said earlier, we aren’t using the self argument in this case, so we don’t need to pass any argument to it.
Okay, so what is the big deal you are asking? I mean, all I did was show you a more complicated way to make a function.
So I am going to attempt to show you a practical use for a class without resorting to totally impractical examples or overly complicated ones.
myExp works by asking you for a number when you create an Instance. Then when you call the exp1() function, it raises what ever value you give exp1() by the number you started with.
- ms = myExp(2) – self.x is set to 2 in the __init__ method
- ms.exp1(3) – 3 ** 2
- ms.exp1(4) – 4 **2
- ms1 = myExp(3) – self.x is set to 3
- ms1.exp1(3) – 3**3
- ms1.exp1(4) – 4**3
- ms.exp1(3) – 3** 2 — this is the cool part. –self.x for my ms object is still 2 and self.x for ms1 is 3
Here it is in running code:
I can create at many Instances as I want, giving each instance whatever value of self.x I want. And I can reuse them in any fashion I can imagine.
Here I pass one Instance as an argument to another.
It is the re usability and the ability to assign different values to objects that makes them so useful.
This is just an intro
Don’t worry if you are little confused. This is just an intro and I will create more OOP lessons to hopefully help clear everything up.
2 thoughts on “Python: Object Oriented Programming”
Thank you, very well explained! Bravo!
Thank you. I have more to come on this topic.