[no audio]
Friends here our concept is Introduction to OOPS.
See basically Python
is supporting for both procedural and object-oriented programming concepts.
Suppose if you implement any code in terms of functions, then we
can say that we are following Procedural Oriented Programming concepts,
and we have already done this in our previous videos.
Now, here our concentration is on OOPS. Then what is OOPS or
what is an Object-Oriented Programming Concept?
[no audio]
See, it is all about usage of class, object, inheritance, polymorphism,
data abstraction, and data encapsulation.
See suppose
if you are new to programming or OOPS concept, then you will
get confused about all these terms. Don't worry.
You don't need to remember all these things, we'll go with
step-by-step so that you will get an idea on each and every
term, or on each and every concept in your OOPS. But for
time being just remember that, OOPS is
all about the usage of class and object. That's it.
Simply OOPS is a combination of classes and objects That's it.
[no audio]
And the very first important point in your OOPS concept is,
we are using OOPS concept to create an object.
Then what is an object?
See object could be anything which exists in real-time like
human, fan, car, mobile, laptop, mouse, keyboard, or any application
or Jenkins, WebLogic, Tomcat, Apache, or Docker, anything,
or Docker pods, anything you can take as an object.
The only thing is if we take anything as an object it should
exist in real-time. That's it.
And if you go with any object, each object
has some characteristics and functions.
Let's say human or person as an object.
Then, we can say that characteristics are like name, age,
height, color. And functions are like walking, talking and running.
Right. Not only human, you can take suppose 'Apache' as an object.
Suppose if I take Apache as an object,
then I can say characteristics
of 'Apache' is like version, configuration, file of Apache,
that is 'httpd.conf'.
And functions I can say, suppose you know 'Apache' is used
to serve some static pages, that I can treat as a function. But don't worry.
you don't need to remember about characteristics and functions.
By default, while writing your code
you will come to know what you have to take as characteristics,
and what you have to write as a function.
Actually, characteristics are like attributes or variables,
and functions you know, to implement some logic we are using functions.
That's it. But you don't need to remember even these
characteristics and functions for your object.
So here you have to remember that object is something
which exists in real-time. That's it.
[no audio]
Right. So why we are using OOPS?
The first thing is, we are using OOPS concept to create an object.
Then, why we need to create an object?
What is the purpose? Right.
Let me give you the very first point
that is very, very useful.
That is, to group related functions.
To group related functions. Why we need to group related functions?
Let me give an example.
Just assume that you are having two applications in your code.
I mean, let's say you're having some web application.
So in a web application, you're having two apps, let's say
'Ap1' and 'Ap2'. And in your code, for 'Ap1' you define 100 functions.
Just for logic implement. And for 'Ap2' also you
implemented 100 functions.
So, this is for 'Ap1' and these are for 'Ap2'.
So totally you are having 200 functions.
Totally you are having 200 functions.
Just observe that in my logic somewhere
I am calling 'Ap2', 'Ap2_f2' function. Then how your Python
is going to execute. Actually, whenever if I call this function
'Ap2_f2' function, then it has to call 'Ap2',
let's say 100th function. In 'Ap2' I want to call 100th function.
Then your Python has to call this last function.
But how is your Python is going to execute that? How is it going to
pick this function whenever we call in this way?
So guys what I am saying is, let me write here so that you will get some
idea. See I'm having two apps in my code.
Then I am implementing 'app1_f1', likewise,
I am implementing 100 functions for my 'app1'.
[no audio]
'app1_f100. The same way I have 'app2' functions in my code.
Let's say, 'app 2_f1', likewise I define 'app2_f100' function,
100th function. So finally how many functions
I'm having? 200 functions in my code.
So I'm having 200 functions in my code, and somewhere
in my code I am calling 'app2_f100' function.
Just I am calling somewhere in my code this function.
Then how is your Python going to execute that?
See actually, you're having, here 100 functions and here 100 functions,
so totally you're having 200 functions in your code, be clear.
So, whenever if I call this function, how your Python is going to execute
or actually, whenever I call this function you have to execute this,
but how is your Python going to execute?
It will take this function name,
and it will compare with the very first function.
Anyway, there is no match. That's why
it'll skip that. So the first check is a fail. Then we'll check with
the second function, fail, check with likewise
it will check 200 checks. In the 200th check, there is a match
that's why it is going to execute this function.
Now, simply to execute this function
your Python is doing 200 checks.
[no audio]
Right. So instead of that
what I am doing is, let me do in this way.
[no audio]
I am defining these 100 functions for 'app1'.
I am giving some name called, 'app1'. Under that
I am defining 100 functions.
Let's say for 'app2' also I am defining a name called 'app2',
I mean I'm creating two separate blocks.
In 'app1' block I'm having 100 functions,
and in 'app2' I'm having 100 functions.
Right. Now, let me remove this.
[no audio]
Just assumption guys so that you will get an idea.
[no audio]
Now, I am calling the 'app2_f100' function,
then how your Python is going to execute?
Previously to execute this 'app2' function, 'app2' 100th function,
your Python had done 200 checks.
[no audio]
Right. Now, how it is going to check? Whenever if you have a
block concept in your code, under block
I am defining 100 functions here, and under this block
I define 100 functions.
So, whenever if you have a block concept here how is your Python
going to execute your function?
So first it will take the first part.
What is that? 'app2'. With 'app2' it will check here.
Anyway, this check is fail.
That's why it will skip all these functions to check.
So then it is coming here
'app2', yes. There is a map with 'app2', 'app2'.
Now, your Python will try to execute your function by comparing in this block,
so that you are skipping suppose 100 checks here.
[no audio]
Right. So, previously you are having 200 checks to execute your
function, but now we are having only 102 checks.
So one check is, comparing 'app2' with block1.
That is failed. One check. Then 'app 2', that is success. Second check.
Then to execute your 'f100' function in 'block2', we need 100 checks.
Anyway, compared to your previous procedure
now we reduced 98 checks.
That is an advantage for us, right? Speed is high, compared to that.
[no audio]
Now, writing your code in terms of a block is called simply object.
That's it. Writing your code in terms of block is called an object.
But anyway, this is not perfect.
I will give that detailed structure.
So now you got to know that OOPS is useful to group related functions,
so that you are going to save some time while executing
your code. Then the second thing is to create a
template or blueprint. What is this?
[no audio]
We know generally what is meant by template.
[no audio]
Just assume that there is some institute,
right, and it is going to give some training.
And just assume that there are four members, they are interested
to join in this institute for some courses.
[no audio]
Right. Whenever these four members are ready to join,
then this institute will give some forms to collect the data,
information about these four students. So, what they will do?
Let's say this is student 1, student 2,
and student 3, then student 4. Then they will give you a form
for this. They are going to give one form for student 1,
student 2 and student 3, then student 4.
Then student 1 will fill the data here, student 2 fill the data here,
student 3 fill the data here, student 4 fill the data here.
[no audio]
But if you observe here, actually
they're having one original template.
They're having one original template.
So, from this template, they're taking a Xerox copy or printout,
and that printout they're sharing
so that the work is easy for this institute.
[no audio]
Now, student 1 fills the data like name, age, address, and interested course.
So, in template, if you have like "Fill the name" here, then "age" here,
then suppose "address", then "interested courses".
Just assume that there are
[no audio]
predefined template is there.
I am giving this template to each and every one
so that I can get the data of these four students very easily.
Suppose if I give an empty form,
then the institute guy has to tell, please fill your name, age,
role number, something like address and interested courses.
But instead of that if the institute guy has some template,
then it is very easy to collect data about students.
[no audio]
Right. So, now that template is called simply blueprint.
So from the template, we are getting the information about your students.
Not only these four students, suppose tomorrow 100 members
are ready to join, then what these guys will do,
the institute guys will do? Simply they will take 100 printouts
of the template, whatever they are having, and these 100 templates
they're going to distribute it for each and every one,
so that they can collect the information about 100 students very easily.
[no audio]
Right. Now in our OOPS concept, template is nothing but a class.
Template is nothing but a class. From the template you can
create any number of objects.
[no audio]
From the template, you can create any number of objects very easily.
Right. Now, I will explain this with a simple example
so that you can get an idea on OOPS concept. Just wait.
One more thing, finally you can say that OOPS is a concept
where characteristics and functions of real-life
object is packaged as a single entity in the code.
Just now we have seen previously, right,
I mean to group related functions.
We are giving some name for functions or simply we are
grouping our functions under one name,
so that there is an advantage while executing your code, right?
See, just assumed that. Let me give a name as, I mean, script name
'working_with_oops.py'.
Just assume that guys. Just assume that
on my host, there are two Apaches running.
[no audio]
Maybe any number of Apaches, no problem.
But initially just to give some idea
I am assuming that on my server
there are two Apaches running.
Just simply assume that. So if two Apaches are running,
if you want to implement a code you need to create two objects.
So if you observe, if you, previously we have seen right,
how we are creating objects? From template.
From template. See that?
So guys, whenever if you are writing a code in terms of OOPS,
and one more thing, basically this OOPS concept is very,
very important for application development or development side.
For automation side,
you can write your code without OOPS concept also,
but if you go with OOPS concept, then you can write your code,
I mean effective code you can write.
Right. Fine.
So guys what I am saying is, just assume that on your server
two Apaches is running, let's say 'tomcat'. Anything you can take.
Let's say I'm taking in this way. In my code two 'tomcats' are running.
Right. See, how I am going to implement my code.
So suppose if your server is Unix-box, then I can say 'bin/python',
then I can start my code in this way, right?
[no audio]
You know, what is the usage of this, '__name__', '__main__'.
[no audio]
Right. Then 'def main()'. That's it.
Now let's say I have suppose 'tomcat7', somewhere
in my location 'home', let's say some,
'/Automation/user', under that we have 'tomcat7', under that
you have some 'conf' directory,
under that, you have some 'server.xml' file.
So why I'm taking 'server.xml' means, if we go with the 'tomcat'
the main configuration file is 'server.xml'.
The same way, just assume that I'm having one more 'tomcat' on my server.
[no audio]
Right. So same values but except
[no audio]
7 this is 9.
So guys while going forward we can also write,
we will also write a code automatically to identify how many
'tomcats' are running on your host.
But for time being I am taking two 'tomcats'. Just I'm assuming,
on my server two 'tomcats' are running. That's fine.
[no audio]
Now actually my intention is,
I have to write a logic
I have to write a logic just to display 'tomcat' configuration file,
and 'tomcat' home.
So already I have taken 'tomcat' configuration file. This is.
[no audio]
Okay. And if you know about 'tomcat', this is called simply
'tomcat' 'home'. Only up to this path
we can say that is 'tomcat' 'home', because under this we are having
every file, I mean the files which are related to your 'tomcat'.
So for 'tomcat7' this is the 'home'.
For 'tomcat9' this is the 'home'.
Now my requirement is, simply I need to display 'tomcat7' config file,
'tomcat9' config file, and 'tomcat7' home, 'tomcat9' home.
Right. Directly I can display, but I want to implement this code first
in terms of functions,
then I will try to convert in terms of OOPS, so that you will get an idea.
Okay. So just wait.
So what I am doing is 'tomcat7_details()'.
[no audio]
Otherwise what I will do is,
'get_details_for_each_tomcat()',
[no audio]
'each_tomcat'
Let me pass 'tomcat7' here.
The same way I need to get details for each 'tomcat', 'tomcat 9'. That's it.
Right. So finally, I am doing 'return None'.
[No audio]
Now here I'm writing a function.
So I need an 'os' module here. Let me import that.
So after that what I am doing is, I am defining a function
called 'get',
Guys let me write.
[no audio]
Let me take function name.
[no audio]
And whatever you're passing, that is configuration file.
So that is like 'server_xml'.
I am taking 'server_xml'.
That's fine.
So now what I am doing is, first of all I am finding
[no audio]
'tomcat' configuration file, 'tcf' variable. It is nothing but directly
you can assume your 'server.xml'. Then I am finding 'tomcat' 'home'.
It is nothing but 'os.path.dirname(server_xml)'
file. But if I do this operation, you are
going to get up to 'conf'. But I need up to 'tomcat7'.
If you come back two levels back in configuration file, then
you are going to get 'tomcat', 'home'.
So I need to apply one more time 'os.path.dirname'.
Already we've done this in our previous videos.
That's it. Right.
And here if you want to display your information, directly you can display.
So I am writing, "The tomcat config file is".
So, simply here, I am writing 'tcf', 'tomcat' configuration file.
And then I need to display 'tomcat' 'home' as well, right?
So "The tomcat home is".
So here I am displaying 'tomcat' 'home'. That's it.
So finally I am returning with "None" value. That's it.
Let me run this code. What happened?
[no audio]
no attribute 'dirname', directory name.
[no audio]
I imported right.
[no audio]
'ntpath'
[no audio]
Where is that error?
[no audio]
That's fine.
'os.path.dirname', 'os.path.dirname'.
[no audio]
What happened here?
Let me enter into my 'python', 'import os'.
[no audio]
'dir(os.path)'
Let me write, and see the options.
[no audio]
It is there no, 'dirname'.
Did I make spelling mistake? No, 'os.path.dirname'. Oh, sorry.
Here, spelling mistake.
That's fine.
Now see the result.
You are getting the output for your first 'tomcat''
I mean, 'tomcat7' and 'tomcat9'. But here, if you observe
in the same function
I am finding my configuration file and 'tomcat' home,
and also I am displaying. But I want to display the information
using other function. Then what I am doing is,
[no audio]
then I am writing 'display_details'.
[no audio]
That's it.
Okay, now if I run this, anyway you're not calling.
Let me call that.
So after getting details for 'tomcat7', I am
trying to display the details for that.
So simply, 'display_details()'. On 'tomcat7'
'display_details()'. But if I run this you are going to get an error
that is, 'tcf' is not defined.
See you know, in your functions concept be clear.
It is very, very important. In your normal functions concept
if you define any variable inside of one function, then you
cannot access that inside of other function.
The reason is, whenever you define any variable,
by default that is local to that function.
Now, I want to access in this function, then what I have to do?
I need to make it 'global'.
Your two variables 'tcf' and 'th', I need to make it as a global.
Now, I am able to get your values.
[no audio]
Right. That's fine.
Now what I am doing is, I am removing these two functions
to display your details
First I am calling your 'get_details_for_each_tomcat()'
- 'tomcat7' and 'tomcat9'.
So, after this I am calling 'display_details()'.
[no audio]
I am calling two times.
[no audio]
Now, what is the result you're going to get?
Both the times you are going to get 'tomcat9' details.
Then where is 'tomcat7'?
So, this is the problem with your normal functions concept.
[no audio]
See, whenever if you call, if you call,
your 'get_details' for 'tomcat7', you're coming here
and you're getting your variables called,
variables called 'tcf' and 'th'.
Because of 17th line you're going to execute this function.
And after executing this function in your variables called 'tcf' and 'th',
you are having values for 'tomcat7'.
That's fine. Then immediately you're calling 'tomcat9' also.
Then whenever if you call, then you are coming to same function
and you are going to overwrite or rewrite, new values
for your 'tcf' and 'th'.
[no audio]
Right. Then your old values gone. 'tomcat7' information gone.
[no audio]
Then, to capture your 'tomcat7' details before calling 'tomcat9',
you have to return your 'tomcat7' details with the help
of 'return tcf,th'. And that values we can assign into some
list or some variables here.
I mean, let me write in this way
so that you will get an idea.
So what I am doing is, 'tomcat7_tcf',
[no audio]
and 'tomcat7_th'. Then I need to return here, right,
'tcf' and then 'th'. But I don't want to do this thing.
I want to capture my values without returning in my code.
Then it is not possible with your normal functions concept.
[no audio]
Then, it is possible with your OOPS concept, right.
Let me write one more program with OOPS concept
so that you will get now an idea
[no audio]
So, 'with_oops.py'. Guys be clear.
First thing, anyway, let me start in this way.
This is common.
[no audio]
Sorry, not that.
[no audio]
Then I am defining 'main()' function.
[no audio]
That's fine. And inside of that, first of all, I am taking configuration files
of 'tomcat7' and 'tomcat9'. Okay.
[no audio]
Or you can also take name like 'tomcat7_cf',
'tomcat9_cf'. No problem. Any variable. Just
I have taken previously 'tomcat7', 'tomcat9',
but I am taking the 'tomcat7_cf' and 'tomcat_9cf'. No problem.
That's fine.
Now let me write simply, 'return None'.
So this is just like your functions. Now in this I want to
include OOPS concept.
So guys at the end of this explanation
you will come to know the usage of OOPS.
And one more thing, whenever if you want to write OOPs concept,
in your code, first thing is you need to define a template.
So, if you want to define any template, you have to take a
keyword called 'class'.
So, what is our concept?
On 'tomcat' we're working. Take any name for your template. That's it.
And whenever if you're defining a template name,
just start your name with capital letter.
That is optional, but it's good to start with capital letters. That's it.
So guys, basically you need to define a template
before going to use OOPS concept in your code.
But instead of calling it as a template, just call it as a
class, defining a class.
But in your mind just assume that defining a class is nothing
but preparing some format, preparing some template.
That's it. Right.
So now one more thing, we know that creating template is nothing
but grouping some related functions into that.
[no audio]
To group related functions we are using OOPS concept.
To group related functions we are creating template.
To group related functions we are creating class.
So now basically in your previous code what are the functions you need?
'get_details_for_each_tomcat()',
and one more thing is, let me take only functions names first.
One function is, this one, right?
And second function is, 'display_details()'.
So be clear.
Now you're getting clarity on your OOPS.
That's fine.
So guys one more thing. Whenever, if you define any function inside of a class,
then you have to say that that is a method,
not a function. Of course, both are same except one extra keyword.
See whenever you're defining a function in your
class, then you have to say it as a method and you have to
write one default argument at the very first always that has a 'self'.
See in your previous code, your function is taking
only one argument. Here
also it is taking only one argument. Forget about this. But
you have to write it. Why? You will get clarity.
The same way 'display' function is not taking
anything in your previous code no.
Now here also it is not taking anything, but you have to write 'self'
first. Write 'self' and forget about it.
Assume that there is no extra keyword here.
'self' and 'self', here and here. That's it.
Okay, but there is a purpose for 'self'. Not only 'self', you can
write any keyword as a first argument, and you have to use
that same argument in your code.
I can write simply my name, but universally all are following 'self'
as the first argument by default, in your method of a class. That's fine.
Now, go to your previous code.
So now here we don't bother, about global or like.
Okay. So I need to define our very first variable called 'tcf'.
Let me copy these two variables.
[no audio]
So, this is the normal function. Under normal function
generally we are defining variables in this way.
But whenever if you define variables inside of a method
always you have to write your variable with 'self.tcf',
I mean you're required variable.
[no audio]
'self.th'. Why? Again, you will come to know. Just wait.
Okay. See normally in your functions if you want to define
any variable, you're writing x=2, y=3, something like that.
But here just assume that instead of writing, 'x', 'self.x', 'self.y'. That's it.
Right. And finally write, 'return None'.
[no audio]
Then, 'display' function. So in 'display_details()' you are having
this thing no. Let me copy the same lines.
[no audio]
Let me arrange indentations.
[no audio]
So whenever if you want to use your variables inside of methods,
always you have 'self.'.
That's why you have to write here 'self.'.
'self.'.
[no audio]
That's it.
Okay. That's fine.
Now, be clear. Just now we created a template.
[no audio]
Right. Like institute is having a template, and whenever
students are ready to take some courses from institute,
institute is giving some template to each and every student.
So, student is going to fill their data into that template.
Now, if you observe in our code we have two 'tomcats'.
Assume that these 'tomcats' are like two students,
and this is like a form for institute.
Now, we need to pass this,
we need to give the copy of this 'tomcat'
template to 'tomcat7', as well as 'tomcat9'.
That's why, see that? I am going to create an object.
I am going to create objects, even forget about your configuration files first.
[no audio]
So, we are assuming that on our server we are having two 'tomcats'.
That's why we need to get two templates into your hand.
I mean, if there are two 'tomcats', we need to create two objects.
How to create an object? Very simple. So we are having 'tomcat7',
right, and then 'tomcat9'. Then first take your required object names.
'tomcat7='.
Guys, to create an object
what we have to do is, we have to take a Xerox copy of template.
So, what is our template? This is our template.
So, what is the name for the template? 'tomcat'. That's it.
Simply take here 'tomcat', but you have to write parenthesis here.
Then create an object for 'tomcat9'. 'Tomcat()'. That's it.
So same Xerox copy of this template
we have given for 'tomcat7' and 'tomcat9'.
'tomcat7' and 'tomcat9'. As of now, we have given Xerox copies to the students.
Now, if students fills the data in the template,
then we are having information about that student in the template, right?
So as of now, we are not creating an object. Simply
we have given the template copy to two objects, 'tomcat7' and
'tomcat9'. Now actually, if you want to create an object,
you need to fill the data.
Fill the data means, you have to call at least
one method from your template.
If you want to call all methods, no problem.
At least you have to call one method,
then we can say that object has been created.
Now, 'tomcat7'.
'.'. See guys if you want to call any method
inside of your object you need to use "."
First thing, be clear. This entire template is there in this
'tomcat7' and 'tomcat9'.
Right. So whenever if you are working with 'tomcat7' you have to
assume that this entire thing is for 'tomcat7'.
And whenever if you are working with 'tomcat9', then you have to
assume that this entire thing is for 'tomcat9'.
[no audio]
Observe that. I want to get my, for 'tomcat7'
I need to get configuration file, and 'tomcat' home.
Then what I have to do is, I need to call this function, method just by passing
[no audio]
this 'conf' because that is taking, configuration file, right?
[no audio]
Now whenever if you call in this way,
Python is going to convert internally this as, be clear, be clear.
[no audio]
Don't write this in your code.
You will get an error. But python will convert that internally in this way.
[no audio]
'tomcat7'
[no audio]
That's it. Let me enable it.
Actually, you should not write. So the same function, this line,
this line is going to convert into, in this way.
So whenever if you convert in this way,
now you can assume, whenever if you are calling this method,
right, what is the 'self' value,
'self' is nothing but now 'tomcat7'.
Now in the entire template replace 'self' with 'tomcat7'. Now what
you are doing? 'tomcat7.tcf'.
Of course, that is 'self', but 'self' is 'tomcat7'.
So, 'tomcat7.tcf'.
So, in memory somewhere
your Python is creating 'tomcat7.tcf',
[no audio]
and at the same time 'tomcat7.th'.
Now, two variables dynamically created for your 'tomcat7'.
[no audio]
The same way in case if we called 'tomcat9', for 'tomcat9' also,
your, in memory somewhere, because while calling
from 'tomcat9', while calling 'get_details_for_each_tomcat()'
from 9, 'self' is becoming 9, 'tomcat9'.
That's why, whenever if it is executing this method for 'tomcat9',
again dynamically two variables are created like
'tomcat9.tcf' and 'tomacat9.tf',
'tomcat' home. Sorry, 'th', 'tomcat' home.
But if you observe in your code, we are not having four variables.
We're having only two variables. But while running, separately
your four variables are created. Then how they are going to create?
Just wait.
[no audio]
First, let me write.
Let me call for 'tomcat9' also.
[no audio]
So, while calling from 'tomcat9', configuration file is this one,
and you have to call from 'tomcat9'. That's it.
Okay. But once again, whenever if you call any method from
object, internally your python will convert in this way.
So, while converting in this way by default 'self' keyword is always
pointing to from which object you are calling,
so that separate variables are created, separate variables
are going to be created for each and every object.
Now, there is no overlap.
See previously if you observed your normal functions concept if
you call this 'get_details' for 'tomcat7', you're getting
information in these two variables for 'tomcat7'.
But if you call from 'tomcat9', if you call from 'tomcat9'
then your variables are overwritten with 'tomcat9' data.
'tomcat7' data is going to delete.
But here that is not the case. Whenever if you call from 'tomcat7',
'tomcat7' variables are dynamically creating,
and whenever if you call from 'tomcat9',
'tomcat9' variables are dynamically creating.
[no audio]
Right. Fine.
So guys, now we are saying that 'tomcat7' object has been created,
'tomcat9' has been created,
somewhere in memory, right. See that.
[no audio]
Somewhere in memory 'tomcat',
I'm not writing full 'tomcat', just simply writing 't7' and 't9'.
So, 't7.tcf' variable,
't7.th',
and 't9.tcf', configuration file and 't9.th'.
So, these variables are created, but these variables are there under this name.
So, this is called an object. Under this object, these two variables are packed.
Right. So, if you want to get these variables, always
you have to get 'Tomcat()' object.
So, simply 'tomcat.', 't9.tcf', 't9.th',
or 't7.tcf', 't7.th'.
Right. So here if you observe, dynamically we are
creating our variables.
Right. And see,
and one more thing, very important one.
If you want to access these variables in other function,
in normal functional programming concept, what we are doing?
We are making our variables as global,
but in OOPS concept you can access this variable
inside of other function without
making this as global, because these two methods
we group under 'tomcat'.
[no audio]
That's why one method variables you can access
inside of other method variable.
So, here we are reducing one step, making global.
We are not making global. By default
we can access one variable of one method into another variable.
That's why directly I have done 'self.tcf'. Right.
So, guys now after executing these, I mean from 'tomcat7' this method,
and 'tomcat9' this method,
in memory somewhere
we are creating four variables, two for 'tomcat7' and two for 'tomcat9'.
Right. Be clear, how they are going to create.
So, simply one memory location for 'tomcat7',
and just you have to call this as an object.
And one memory location for 'tomcat9', and this is an object.
And in this we have, 'tcf' variable and 'th' variable. The same way here
also we have 'tcf' variable and 'th' variable.
If I want to get 'th',
I mean 'tomcat' home, 't7.th',
I have to use. See I'm not calling display externally,
outside of your class, inside of my 'main()' function.
Whenever you are in the inside of a class, then only you have to take 'self'.
Be clear. Whenever you are inside of a class,
then only you have to take 'self' keyword.
Whenever if you are in the outside of a class,
you have to take your object name.
So, 'tomcat9'. For 'tomcat9' we have two variables.
What are those two variables? 'tcf' and 'th'. But I want to display them,
then, '.tcf',
[no audio]
then print. '.th'. See the result.
[no audio]
Something wrong. What is that? Let me verify.
[no audio]
'os' is not defined. Yeah, we need to import that 'os'.
[no audio]
Let me 'import os'.
Now run the output. See the result.
You are getting 'tomcat9' details.
Now I can print randomly.
First, I can print 'tomcat9.tcf', then 'tomcat7.th', and then at last
I can print, 'print(tomcat7.tcf)'.
Yes, you can access at any time, randomly
your required variables.
Because somewhere in memory location they're going to create
a new variable for each and every object.
They are not going to overlap.
'tomcat9' is separate memory location,
'tomcat7' object is separate memory location, right?
So, this is the way how we can access your variables outside of a class.
But we already defined a function inside of your class to display your variables.
Now, I want to display for 'tomcat7' configuration file and home,
then I need to call this 'display_details()' method from your
'tomcat7'. Let me call that. 'tomcat7.display_details()'.
No need to pass any arguments.
Anyway, by default 'self' is there, right.
We need to pass 'self' value generally,
but whenever if you run in this way,
Python is going to convert your 'display_details()' in this way.
[no audio]
'tomcat7'. That's it.
Because we are not having any arguments here.
Now this 'tomcat7' is going to assign 'self',
and inside of this, what we are having? 'self.tcf', means
we are calling from 'tomcat7', right.
That's why you are going to get 'tomcat7.tcf', 'tomcat7.th'.
Right. See that. I'm going to run.
See the results. You're getting 'tomcat7' details.
Now, let me call for 'tomcat9'.
[no audio]
So, whenever if you are calling from particular object, based
on that your Python is going to get variables for that object.
See that. And I can call any number of times.
First, I am going to call 'tomcat9.display_details()'.
See that.
So this is the advantage with your OOPS concept. Right.
So, guys once again I am repeating, the important
points for OOPS concept is.
First, we are taking OOPS concept to create an object.
So, in our program we created two objects.
[no audio]
So, what is the advantage by creating two objects?
Dynamically we are going to create a separate memory location
for your required object
so that we can access any variable at any time.
[no audio]
Right. And basically OOPS is nothing but
grouping related functions for particular thing
under one name, that is called a template.
Now, in reverse way
we are creating template to create an object.
Under template we are writing the related functions for that object.
That's it.
[no audio]
Right. And one more thing,
no need to write always 'self' here.
You can write anything.
Let's say I am writing instead of 'self',
what I am doing is, so I will write suppose, "narendra".
So, I am going to replace all with "narendra" only.
Then it will work. No problem.
The only thing is, universally we are following one rule. Instead
of taking any random default argument,
we are taking 'self'. But we can take anything.
Let me write.
[no audio]
So, I'm going to replace all with "narendra".
[no audio]
Actually, that is my name, right. Now, see the result.
There is no change.
[no audio]
So, but generally, universally we are following.
[no audio]
See, if I go with my name,
if you go with your name, you will get somewhat confusion.
That's why universally we are following default argument as 'self'.
That's it.
So, make sure that in the interview suppose if they ask, "What is
the default argument for methods in class?"
Then you have to say that, we can take anything,
but universally we are following 'self' as the default argument.
That's it. Okay.
I hope you are getting at least some idea to use OOPS concept.
Anyway, while going forward we will write some real-time scripts
by taking some real-time scenarios
so that you will be good with your OOPS concept.
And one more very, very important point is, in case
if you are not good with OOPS,
don't worry at automation side. But if you are going to plan
for application development or any GUI applications,
something like that, you should be good with OOPS. That's it.