WHAT IS OBJECT-ORIENTED PROGRAMMING/OOP?
Object
oriented programming, as the name suggests, is programming using different
kinds of objects. We create an object using something called a class. A class is like a blueprint for creating
objects. In Python, every object is an instance of a class, which means the
object’s data type is the class that created it. For example, the data type
‘String’ is actually a class named ‘String’ in Python and so are all the other
data types. Think of class as parents and objects are its children.
A class name
should always start with an uppercase letter. We should always use camel case in class name, which means if a
class consists of multiple words, the first letter of every word should be
uppercase. This is usually done because spaces are not allowed in class names.
For example, ‘ThisIsCamelCase’
Let’s take a
look at a piece of code to understand OOP better:
class Orange:
def
__init__(self):
print(‘Created’)
apple=Orange()
So the
explanation can seem a bit complicated at first but I am sure you brilliant people
will get it in just a minute. So let’s give it a try. By the way, if you have
no idea what I just did here, or don’t know what a method/function is, you
should check out my article on modules here, it will not take much of your time, probably ten minutes and then you can
come back right here.
Here,
‘Orange’ is the class name, and inside the class we defined a method called
‘__init__’ and passed in the parameter ‘self’. Now if you type this code in
text editors like Sublime Text, you will notice that the colour of the method
is different from other methods and parameter ‘self’ has its own colour as if
it is a valid parameter by itself. This is because this is not an ordinary
method, it is a magic method (I know
LOL). All of this will be clear in just a moment. We defined a method which
will print ‘Created’ when called. ‘apple’ is the object that calls the method
‘Orange()’ (again this would be clear if you check out my article on functions). If you are familiar with
calling methods, you know we are supposed to pass in parameter if we
initialized the method with a parameter and we did initialize it with ‘self’
but surprisingly we didn’t pass in anything when calling it. All of these will
be crystal clear to you in the very next heading.
WHAT IS A MAGIC FUNCTION?
In Python,
when you name a method ‘init’ preceded and followed by two underscores, you are
defining a special method that Python calls every time you create a new object
using that class. For example, in the above example, when you create an object
using the class ‘Orange’, Python automatically calls and executes the
‘__init__’ method as well. It’s like you don’t have an option.
Any method surrounded by double
underscores is called a magic method. A method Python uses for special purposes like creating an
object.
When you
define a method, it must always accept at least one parameter (except in
special cases). The convention in Python is to name this parameter ‘self’. (Do
you see all the dots connecting now?)
When you
define a method with ‘self’ as a parameter, Python passes the object that
method was called on back to it as a parameter. For example, when you called
the class ‘Orange’, an object was created which was stored in the variable
‘apple’. Since we used ‘self’ as a
parameter while defining the method ‘__init__’, Python now passes that object
as the parameter to the ‘__init__’ method. (I hope you see now why the
parameter is called ‘self’)
When you
create a new object, you don’t have to pass any parameter for ‘self’ as Python
automatically does it for you. (Just like we did in our ‘apples and oranges’
example)
CREATING INSTANCE VARIABLES:
You can
create instance variables using self. Instance
Variables are basically variables that are exclusive to an object. If you
want to use these variables, you must first create a specific object and then
use that object to access these variables. So how do we create them using
‘self’? Let us find out:
class Fruit:
def
__init__(self,w,c):
self.weight
= w
self.colour
= c
This is the
code to create an instance variable. As always, let us break it down.
When the
class ‘Orange’ is called, it will create an object (as usual), but this time it
will assign two instance variables (weight and colour) to it. So whenever we
call the class ‘Orange’ we must now assign values to the variables ‘weight’ and
‘colour’ respectively. For example,
orange = Fruit(2, ‘Light Orange’)
So now the
object ‘orange’ has the values ‘2’ and ‘Light Orange’ assigned to it as weight
and colour respectively.
Now, you can
call these variables anytime, using ‘orange.weight’
which will print ‘2’ or ‘orange.colour’ which will print ‘Light Orange’.
Note: An instance variable should always be
defined inside the method ‘__init__’ except in special cases. And just in case
if you are wondering, the method ‘__init__’ is not some random name of a method
I came up with. It is a specific method. You can check this by typing in ‘def __init__’ and ‘def __random’ in Sublime Text 3 and notice the difference in the highlighting colours.
You can also
change the values of instance variables just like any other variables. For
example,
orange.colour = ‘Dark Orange’
Now the value
of ‘orange.colour’ is changed from ‘Light Orange’ to ‘Dark Orange’.
CONGRATULATIONS !!! You now know what OOP is?, how to create instance variables? and what is a special method? Like I always say, Have fun with it !!! Create some cool programs. Good luck !!!!
So, this was an introduction to Object_Oriented Programming Python. Stay tuned for another article next week, same time, where we will learn about The Four Pillars Of Object Oriented Programming. So more cool stuff coming your way, DON’T MISS IT !!
I hope this article answered all of your questions and even helped you in becoming a better programmer. IF IT DID, leave a like AND FOLLOW THIS BLOG TO BECOME A PROFESSIONAL PYTHON PROGRAMMER FROM A TOTAL BEGINNER. IF IT DIDN'T, feel free to ask any further queries in the comment section below.
HOPE YOU HAVE AN AWESOME DAY AHEAD !!!
0 Comments
Welcome to the comments section, this is where you can contact me personally for any doubts or feedback