[no audio]
Friends, here we are going to
see how to you use functions of one Python
Script into another Python Script, and what is
'__', name variable,
and finally we are going to see how to create user-defined modules.
First, let me start with a simple Python Script.
[no audio]
Let me create a simple Python Script called 'script1.py'.
Now here I am going to define two functions, one is for addition.
[no audio]
Now let me 'print("
The addition of
and is", simply 'a + b'.
[no audio]
Then let me write
'return None'.
And let me write one more function, 'subtraction(a,b)',
so 'print
[no audio]
("The subtraction of and is", so simply
.
That's it.
Now, 'None'. Fine.
Now, these are the two functions.
Now, let us assume that we have two variables.
Let's say 'x' equals to some number,
and 'y' equals to some number.
Now, I want to get the addition and subtraction for these two
numbers. So already we defined functions.
That's why simply I am calling 'addition' function by passing
(x,y), and
[no audio]
next time I'm calling 'subtraction' function
simply by calling your 'x' and 'y'. Now
let me save it and run it.
What happened?
[no audio]
Okay.
[no audio]
Spelling mistake.
[no audio]
Now see the result. You're getting 11 and 3. 7 + 4, 11;
7 - 4, 3 you're getting. That's fine.
Now this is your 'script1'. Now
actually I want to write one more script,
but there I want to find addition, subtraction, and multiplication
of two numbers, right?
So what I can do, one thing directly I can copy these two
functions here and I can write one more function simply for multiplication.
[no audio]
Right.
"The multiplication of and is",
simply ''.
That's fine.
But as of now just for your addition and subtraction you
are having very simple lines.
Just assume that you are writing a code in real time.
[no audio]
Right. So there
you have for a function
some hundred lines. First for 'addition' function just assume
that. First 'addition' function you have hundred lines. For
'subtraction' function you have hundred lines.
Then unnecessarily you are going to copy two hundred lines
of code into your current script from your previous script.
[no audio]
Now what I want to do is, I don't want to copy these two functions
here, but I want to use directly from 'script1'. Your
'addition' and 'subtraction' functions
I want to use directly from your 'script1'.
Now, first of all let me give the second script name as
'script2.py'.
Right. Fine. Now, first of all let me define any two variables.
So I am defining 10 and 20, right?
First of all simply I'm finding multiplication of your 'x'
and 'y' from your current script itself.
Yes, you are getting your result.
But now I need to find addition and subtraction of this 'x,y',
I mean 'x' and 'y' variables. So directly I can write
the logic but my intention is if already function is there
in any other script how we can use that in the current script?
Right. So first of all what I am doing is, I am going to comment
these lines, why I will tell you.
But actually you should not disturb other's script, right?
But as of now just I'm commenting. Fine.
Now we are under 'script2', but we want to use the functions
from 'script1'.
So in that case, what you have to do is first of all at very
first line simply you have to write 'import script1'.
Don't write '.py'. 'import script1'.
So this is 'script1', right?
So we are importing 'script1'.
That's it.
Now, let me run it and see the output. There is no error.
Right. Now in 'script1' you have functions anyway, 'addition'
and 'subtraction', right?
So in case if you want to know what are the functions are
there in 'script1' directly you have a logic called 'dir'.
See that 'addition' and 'subtraction' functions are there.
Right. Anyway, we know that what are the functions are there in 'script1'.
[no audio]
Now what I am doing is from 'script1'
I want to call 'addition'. Now
my values are 10, 20. Then what is the result? 10 + 20, addition.
30, you're getting.
Now from 'script1'
I need to call subtraction as well.
Then you are going to get -10.
That's it.
Right. Now, we are not writing any logic for addition and subtraction
in the current script. What we are doing? Already
we have functions for addition and subtraction in another
script, we are trying to use that logic by importing your 'script1'.
So first of all, this is the way how we can use other script
functions in the current script.
[no audio]
That's fine. But while doing this process we did, we commented this code,
but actually you should not comment because that is someone's
code, right? You should not go and disturb that.
Be clear. Now I am going to run our code.
Let me run it only for
[no audio]
'addition', okay.
First of all let me comment it.
[no audio]
So why I'm commenting, you need to get clarity.
Actually, you should not comment.
First of all, let me run this and see the result.
So addition of 10 and 20 is 30 you're getting. Now, this is 'script1'.
Right. Now your script
is there as it is. We uncommented that code, right?
We don't know what is the logic there in 'script1', but we know only
'addition' function is needed from 'script1'.
That's why simply I'm importing 'script1', and I am calling 'addition'
function from that. Now, let me run it and see the result.
Actually, you need only this output but why you are getting
these two lines?
This is for 'script1' purpose, not for us, right.
Whatever the logic, you are getting output, right.
This is for 'script1' purpose, not for us.
We need only 'addition' for 10 and 20.
We need only this output, why you are getting
unnecessarily these two lines?
Right. So that's why generally while implementing Python
Script you need to follow some
procedure, some steps. They are, let me write it. See first of
all guys, what will happen whenever if you try to import any
script or any module?
This is like importing module, right?
See, whenever if you import 'script1' your Python will
go and execute your 'script1' first.
So while executing 'script1 what will happen? This is the
'script1' code. Your Python is going to execute from top to
down. While executing it from top to down in 'script1' also
we have 'addition' and 'subtraction' calling,
so that's why you're going to get output for these two.
But we don't want that. In our current script
we don't want that, right?
So that's why generally while implementing any script, right,
you have to write some 'main' function, inside of that 'main' function
[no audio]
you need to execute your main logic.
[no audio]
So this is first of all standard way to write scripts
using Python. Right. Now
we are calling 'main'. Even though if you do this still you're
going to get same output. Observe that.
Now I am running, rerunning. See the result. Unnecessarily you're
getting two lines of code, extra code, but I want to get only this line.
I don't want these two lines, right?
So that's why what I am doing is, while calling 'main' function in
'script1' I am writing a simple logic.
[no audio]
Don't worry, first observe the output.
[no audio]
Right. Now see the result.
I am running. Now, you are not getting your 'script1' output.
You are getting only from 'script2' output. This is what we need.
[no audio]
Right. The same way whenever if you are writing code, you
need to follow this structure for your Python,
then that is a good Python script. That's it.
Nothing is there.
[no audio]
So how you are going to overcome getting output from 'script1'?
I will show you, I will explain that.
[no audio]
Now,
that's it.
[no audio]
See that. And go to your 'script1', and run your 'script1'.
So you're going to get your 'script1' output as well.
There is no change in the output. It's working perfectly.
Right. So now you need to understand about name variable,
'__name__'.
Right. Fine. Now, see. First let me comment entire code in your
'script1' as well as in 'script2'.
[no audio]
Now what I am doing is simply I am printing
[no audio]
'__name', that is a variable like 'x', predefined
variable, right?
I am running. See the result what you are getting. '__main__'. The
same way that variable, even I am trying to print from
your second script as well.
[no audio]
You're getting '__main__', right.
Now, what I am doing is,
this is
from 'script1', right. Now, let me run it.
Yes, that's fine.
Now, same way I am trying to write here,
"This is from script2".
Now, let me run it. Fine.
So whenever if you're running your 'script2', and
if you are printing '__name' variable value
you're getting '__main__', or in 'script1'
also whenever if you try to print '__name'
'__name' variable value
you are getting '__main__' only with underscores.
Right. That's fine.
Now, what I am doing is, I am trying to 'import script1'.
[no audio]
Right. See whenever if you 'import script1' first what your
Python will do means, your Python will go and execute completely
'script1' because of this line. Because of this line your
Python will go and execute your 'script1' first completely.
Right. First observe the output.
[no audio]
"This is from script1", but
what you are getting? You are getting 'script1' not 'main'
And because of this line, this is from 'script2', you
are getting 'main'. That means whenever if you run any script
directly, then 'name' variable value equals to 'main' with underscores.
The variable name is 'main'.
But we are running this 'name', this code indirectly from 'script2'
right. 'script1' means, your 'import script1' means
you are running 'script1' code, but you are running
indirectly from 'script2', you're not running directly 'script1'.
We are not running this code directly. 'script2' is running
'script1' code. In that case that 'name' variable value
is equals to 'script1', not 'main.
Be clear.
If you run simply your code directly, and in that case your
variable value is 'main', but if you run any code indirectly,
if you observe here 'import script1' is running because
of 'script2', while running 'script2' you're also running
'script1'. That means 'script2' is running 'script1'.
You're not directly running 'script1'. So at that time whatever
the variable is there inside of 'script1' that variable,
'name' variable value will become the name of that script, right.
Now, observe here.
[no audio]
Now, let me comment this code.
Now because of 'import script1' what will happen? Your code
will run from top to down.
[no audio]
Because of 'script1', 'import script1', I mean from 'script2'
you are importing 'script1'. Whenever if you do 'import
script1', at the time what will happen? Your Python will
run first entire 'script1', right. Now observe that. In
'script1' what we have? So while running your 'script1'
from top to down
function, anyway your Python won't execute as of now, function,
your Python won't execute as of now, function again,
your Python won't execute as of now. Simply
it will remember all these three functions. Now,
this is the 'main' line, 'if __name', so you
are not running directly
this 'script1'. Your 'script2' is running,
that's why 'name' value is equal to 'script1'.
Now this is equals to 'script1', not 'main' now, right. If it is 'script1'
then they are not equal, right? This value and this value,
they are not equal. If they are not equal then 'if' doesn't
allow to execute 'main' function.
It doesn't allow to call 'main' function.
If you are not calling 'main' function, then you are not calling these lines.
If you are not calling these lines, then you are not getting
any output from function one,
sorry from 'script1' in 'script2'.
That's it. So that you are going to,
[no audio]
what you're doing? So that
you are going to avoid getting output from 'script1'.
So you are going to get output only whenever if you call functions.
[no audio]
Right. Now, see that.
Now we are on 'script2', so whenever if you run your Python
Script, what will happen? Very first line 'import'. Yes, that will
execute. 'import script1' is nothing but you are going to run 'script1'.
Anyway, you know in 'script1'
what is the logic there,
right?
Then after execution of all the code from top to down
what your Python will do? Simply your Python will remember
all the functions names.
That's it. Then it will come back to 'script1'.
Now in 'script1'
we defined one multiple,
I mean 'multiplication' function, one 'main' function, right but
be clear, already you know 'main' function from here, but that
is from 'script1', not from 'script2'. Now this is from 'script2'.
Your Python can distinguish that.
Then after that you have
'main'. 'if __name__ == "__main__"'.
[no audio]
Right. Now you are running 'script2' directly, that's why 'name'
variable if it is equal to 'main', both are equal.
If both are equal your 'if' condition will allow to execute
this 'main'. Then you are coming to here. Then 'x = 10',
'y = 10'. Then from 'script1' you're calling 'addition'.
Now your Python will go to 'script1', and there it will try to
execute 'addition' function, then we'll come back to here. That's it.
[no audio]
That's it. So now,
by doing in this way what we are doing?
[no audio]
If you observe, 'script1' is now a module for your 'script2'.
That's why you are importing 'script1' here.
[no audio]
Right. So
guys, now, from now onwards while going forward whenever if
you're trying to import your, sorry whenever if you are
trying to create any Python Script, now you need to follow
this structure. That is in case if you want to import anything
you can import here,
some 'sys', 'os', or 'time', some modules, or you can also import
line-by-line. 'import os'. 'import time'. Right. 'import datetime' module.
Let me save it.
[no audio]
'newstructureforpythonscript.py'. Right. Then after
that very first line is always you try to import, you try to write in this way.
'If __name__ == "__main__"underscore
then call 'main' function. Now try to define somewhere your
'main' function. Now from here onwards try to write your main
logic. Suppose I want to find the addition of any numbers,
let's say 'x = 1', 'y = 2'. Now directly
I don't want to find, or if you want to directly find, yes
simply write 'x + y'. That's it.
Sorry 'x + y'.
[no audio]
Then simply, this is a function
that's why at the end
just write 'return None'.
That's it.
But I don't want to directly write my addition here, then
just try to define one function for that, 'addition(x,y)'.
Somewhere just write 'addition(a,b)'. Then try
to print, "The addition of your and is simply
''. That's it.
So I'm not returning anything.
Simply write 'return None'.
That's it. Now see the result.
We are getting same output.
[no audio]
Right. So guys while going forward, please follow this structure.
Always try to start with your code with 'if' condition,
'if __name__== "__main__", then call 'main' function, and
try to write your logic from 'main' function.
So what is the advantage by writing this means, whenever if
you are trying to import this code into other scripts at
that time you are avoiding execution of your 'main' block.
[no audio]
In case if you don't write this logic, unnecessarily you are
going to execute your code in other scripts.
[no audio]
Right. So, so that's why while going forward we are trying to follow
this structure while writing Python Scripts.
Right. If you write in this way, then this is nothing but
creation of your own modules, or you are defining your required
modules, or this is called user-defined module.
That's it. Simple way
I'm explaining here.
Okay guys, thank you for watching this video.
[no audio]