Python Tutorials - LEGB Rule | Local And Global Scope | global and non local Keyword
ฝัง
- เผยแพร่เมื่อ 11 ก.ย. 2024
- In this Python programming video tutorial we will learn about different variable scope and legb rule in detail.
To understand the decorators we need to learn namespace and variable scope.
Generally namespace (sometimes also called a context) is a naming system for making names unique to avoid ambiguity.
Namespaces in Python are implemented as Python dictionaries, this means it is a mapping from names (keys) to objects (values).
Variable scope refers to the visibility of variables. In other words, which parts of your program can see or use it.
There are 4 types of scopes in python in this tutorial we will discuss about that in detail.
Local scope refers to variables defined in current function.
Enclosing - names defined inside any and all enclosing functions.
Global - names defined at the top-level of a module. Each module brings with it a new global scope.
Built-in - names built-in to the Python language through the special builtins module.
Namespace and Variable Scope:
• Python Tutorials - Nam...
#PythonProgramming #Decorators
For more free tutorials on computer programming
/ amulsacademy
/ amulsacademy
Wow I have been watching lot of videos regarding LEGB but literally u saved me I understand through your video thanks a lot ❤🙌🙌🙌
Nice explanation in my school I don't even know what they taught 😆
Nice Explanation and easy to understand the scopes
Thank you :)
Best tutorial i have watched on youtube. Keep up the good work.
Thank you :)
YOU EXPLAINED VERY WELL MA'AM ..... GREAT PYTHON EXPERT😁👍👍
Thank you! :)
Very well explained, MOST UNDERRATED YT CHANNEL
Thought you were a student when I first listened to your voice mam......And the explanation is ........No need to mention...❤️
Thanks a lot 😊
You voice is make me made programmer . I Love your voice 💓😻❤️❤️❤️.
You are a great teacher Amiul, wish u all the best.
Voice is awesome so sharp it's pierce my ❤️ ... nice explanation 👌
very informative lecture .Thanks
God bless you always.
Always welcome :)
Great work.Thanks a lot for sharing the information.
My pleasure :)
Thank you mam. I'm learning Python this year for my board exam😀
pleasure :)
All the Best :)
Kaisa Gaya paper?
@@Aalekh_Chaudhary I scored high marks in it😄
@@anas5575 are boi expect nahi kiya tu reply dega after 3 year
@@Aalekh_Chaudhary hahaha 😂😂
Thank's for the tutorial amulyaa and the concept delivery is crystal clear and again thanx
You are teaching really awesome... Keep educate us !
Thank you :)
@@AmulsAcademy could you post a video about python multithreading?
I will try :)
mam you are simply excellent thanyou somuch for the vedios
Really Helpful Amulya (y) , Thanks
Pleasure 😊
super explanation about scopes and rules amulya garu...thank u so much ji...can you please make how to find bugs and which tool is best for it in python...
Thank you :)
you are awesome in your explanation.
Explanation is very nice madam
Thank you so much :)
Excellent expalanation sister , thanks a lot
You are most welcome :)
Thanks, didi for helping us........
Most welcome :)
Great explanation
thank you very much, ma'am, the way of teaching is very simple and smooth.
Pleasure :)
Great ❤️ video ☺️
Thank you 😊
explained in the best way.. thankyou🙌
Gzb ki voice hn
nice session di ..thank you so much
Your tutorial about LEGB rule is very helpfull ....☺❤
thank you madam, good explanation
NIce Video Thanks
Thanks mam video banane k liye
you made to understand everything about scope thank you so much(:
Happy to hear that!
@@AmulsAcademy ty can you please teach machine learning algorithms?
Wow how awesome ur explanation is 😍
Thank you so much 😀
Amazing explaination ,I got every point
Thank you :)
good job
Nice explaination mam super
Thanks a lot :)
@@AmulsAcademy Mam can you make video on random function and random module. How to solve random function.
Mam please make video on this topic
Great explanation specially the scope...thank u mam
My pleasure 😊
thanks man! keep it up!
For example if we have x in global, enclosed, and local
By default it will print local value only
But i want to print enclosed/global value
How can we do that
I know 1 method globals() to print global value
But how to print enclosed value
Try this
x=100
def f():
x=200
nonlocals= locals()
def f1():
x=300
x = nonlocals["x"]
print(x)
f1()
f()
You can use globals() for global value 😊
@@AmulsAcademy tq very much
I will try this
thank u so much well explained
Simply superb
Thank you :)
Thanks for the explanation 👍🏻
My pleasure :)
Python's scoping rules is not LEGB - but rather LEMB - the "M" stands for module. Python has no true global variables - or global functions for that matter. Python does not have truly "global" variables. Any variable defined in one module (outside of a function) must be imported into any other module that wants to use it. This is the source of a huge misunderstanding. The use of "global" in python is at odds with most other programming language - making python quirky in many respects. I do not recommend python as a first language. The Microsoft languages in Visual Studio are a vast improvement - in my view...
superb explanation maam
Glad you liked it!
Awesome tutorial.Clearly explained with examples. Thank you.
Glad it was helpful! :)
Thanks for clarification on topics 😊
Welcome :)
I created a list globally and wrote a function to sort the list. When the function runs, sorting happens on the global variable. I mean if I access the list outside the function, it appears sorted. why?
Yes because it is global you can access it inside function body😊
Awesome tutorial thank you so much ma'am.
very nice explanation..keep up the good work
Thanks ur vdo was quite helpful
My Pleasure :)
great!!!
Thank you for the tutorial
You’re welcome 😊
Hi Amulya, In your third explanation for Enclosed scope, am unable to define the function inner after z=4, while printing am getting an error that inner is not defined. Can you help me here?
Give me the program , I will check :)
Now I get it, thanks 👍🏾
thank you ma'am!
Nice explanation,in which company r u working?
Thank you :)
I am a Full time TH-camr :)
@@AmulsAcademy wow great
thanks for the lovely explanation i have a doubt abt how you explain in last lebg rule it was the non local then local then global?
First local then enclosed then global lastly built_in
:)
thank you so much ma'am. The concept has been well-explained with required examples. Thank you for helping us.
It's my pleasure :)
Hiii amrutha
Small help from u
Best ever
Hi...
I am able to update the Enclosed variable inside the function as well as outside.
Here is the code without mentioning nonlocal, could you please clarify
x=10
def fun():
y=15
def inner():
z=20
print(y+1)
print(y+1)
inner()
fun()
In the program you are not changing the y value, you are just printing y+1 value.
when you write y=y+1, here you are changing the y value.
:)
@@AmulsAcademy Thanks for clarifying. Much appreciated.. keep it up.
THANK YOU 😍
I like your videos😘😘. Is this complete python tutorial?
Thank you :)
basic concepts are covered.
Now doing advanced topics :)
Thanks for your reply 😊😊😊
Thankyou.......
Pleasure :)
very good teaching mam. Thank you.
It's my pleasure :)
is global varible is define in outside the function.
Yes :)
Thank u so much!!
You're welcome! :)
y=5
def outer():
x=10
def inner():
z=16
nonlocal x
x=x+1
print(x)
print(z)
inner()
print(x)
outer()
11
16
11
hello madam.! in this program i had modified 'x' in the local scope and it is done but why it is changed in the enclosed scope
please reply mam.sorry if there is anything wrong in my question.please correct it
Because you are calling inner() before print(x) so it will modify x value.
You can try this:
y=5
def outer():
x=10
def inner():
z=16
nonlocal x
x=x+1
print("x value inside inner:",x)
print("z value:",z)
print("x value outside inner:",x)
inner()
outer()
How it is search the variable x in built in scope?
thank you :)
NIce mam.
Thank you :)
Wow
Thank you :)
Clearly explained, thank you very much.
Glad it was helpful!
a = 5
def function():
a=10
print('aE',a)
def inner():
a=15
print('al', a)
inner()
function()
----------------output-------------
aE 10
al 15
in this case it print the enclose value 1st then it come to local scope , but according to the rule LEGB first it should be print local scope variable. kindly rectify me if i'm in wrong
Susant, You are calling the inner() inside the function(), thats why aE 10 is printing first. aE is local to function() and al is local to inner(). In both the case Local is printing as priority.
Perfect.
Thank you :)
like it
Thank you :)
Hello madam, can you make some exercises (python problems) of nested functions
At 2:43 you have to change the order to LEGB
9:57 its we can access enclosing var in local scope ?
Yes you can using nonlocal keyword...
Where did your English learn from? Are you Indian people?
“The reason I talk to myself is because I’m the only one whose answers I accept.”
-
Hii amulya small help from u
How can I help you?
Not understood
you make this so difficult , you can explain better
koi
engadi porandha punda