Skip to playerSkip to main contentSkip to footer
  • 5/21/2025
The scope of a function in Python dictates the visibility and accessibility of variables within the code. It helps prevent naming conflicts and ensures that variables are only accessible where they are needed. Python uses the LEGB rule to determine the scope of a variable:
Local:
Variables defined inside a function are local to that function and cannot be accessed from outside.
Enclosing function locals:
If a function is defined inside another function (nested function), the inner function can access variables from the outer function's scope.
Global:
Variables defined outside any function are global and can be accessed from anywhere in the code.
Built-in:
Python has a set of built-in functions and constants that are always available.

Category

📚
Learning
Transcript
00:00Guys, in this lecture, we have a important concept to discuss about the scope, which is called the scope.
00:07Scope is called visibility.
00:11You can see that you can see the scope, which is called the scope, which is called the visibility.
00:16Scope means that a program is not in the same way that all the variables are accessible.
00:24For example, we will give you a print, let's say,
00:29variable x.
00:32Right?
00:32Now, I will execute this.
00:35And it says,
00:36X name is not defined.
00:38Now, I will see that it is obvious.
00:40You can see that x is defined, declare.
00:45Okay.
00:46So, I will say that x is equal to 10.
00:50Execute.
00:51Error.
00:52Well, you can see that,
00:54you can see that,
00:56you can see that,
00:57so,
00:57anak-macholing not to do it.
00:59Python is basically,
01:02which is visibility is,
01:04which is seven lines,
01:05which is on the top.
01:06It is like this,
01:07you can see that,
01:08sir, python is on the top.
01:11Okay?
01:12Python is on the top.
01:12So,
01:13you can see that,
01:13you can see that,
01:14you can see that,
01:15so,
01:16when you see that,
01:17Python is on the top.
01:18So,
01:18here is this,
01:19you can see that,
01:20so,
01:21here is the error.
01:22print कर देता है
01:23तो basically
01:24यह जो print function है
01:27इसका जो scope है
01:28वो line 7 से उपर उपर है
01:30यानि कि यह जो देख रहा है
01:33इसका जो region
01:33vision of region है
01:36जिस region के अंदर यह देख रहा है
01:38इसकी आँखे खुली हुई है
01:39जिस region के अंदर यह expect कर रहा है
01:41वो region है line 1 to line 6
01:45ऐसा यह ना यह region है
01:48इसके अंदर x को exist करना चाहिए
01:50यह print function का scope है
01:52ठीक है
01:53कि जिसके अंदर उसकी visibility है
01:55इसके बात चाहिए आप x define करें
01:57इसको कहते हैं scope
02:02अब थोड़ा सा
02:03मैं इसको ramp up करने लगा हूँ
02:04एक word उपर जाने लगा है
02:06अब imagine करें
02:08कि
02:09इसको remove कर देता हूँ मैं
02:12और
02:18और यहां पे
02:25मैं एक variable डिफाइन करता हूँ
02:28let's say c is equal to 20
02:29सही है
02:31अब अगर मैं यहां पे print करता हूँ
02:34print
02:35c और execute
02:38करता हूँ
02:39तो c basically visible है
02:42right
02:43however
02:45अगर मैं इस c को
02:48ऐसे ही रखता हूँ
02:50और एक function define करता हूँ
02:51def name
02:52def name
02:54और c
02:55exactly वहीं पे खड़ा हूँ है
02:57अब मैं अगर इसको print करता हूँ
02:59तो यह मुझे कह रहा है
03:01c is not defined
03:02तो आपके ख्याल से अब क्या हुआ
03:05इसका मतलब यह है
03:07कि जब आप function define करते हैं
03:11तो function अपनी scope खुद ले के आता है
03:14और जो function के अंदर variables होते हैं
03:18जैसे कि आप देख रहे हैं
03:20वो basically function से बाहर accessible नहीं होते
03:23function के अंदर यू समझिये कि जो भी code होता है
03:28वो एक خاندाम की तरह होता है
03:30एक घर की तरह हुता है
03:31और घर की जो चीज़ें होती है
03:33वो सिर्फ घर वालों के लिए accessible होती है
03:35घर से बाहर वालों के लिए accessible नहीं होती है
03:37जब भी आप एक code लिखते हैं
03:39तो उस code का structure यह हुता है
03:41के आप एक main function definition
03:43define and inside of all blocks.
03:46Okay, so this is the limit.
03:49However, this level is the indentation level.
03:54Indentation level is the point.
03:57This function definition is the point.
04:00This function is the point.
04:03This function is the point.
04:06This function is the point.
04:08This function is the point.
04:11This function is the point.
04:21This function is the point.
04:23This function is the point.
04:25This function is the point.
04:27However, if we keep it in this loop or in this loop,
04:31it's totally visible.
04:33However, if we keep it in this function,
04:35it's not visible.
04:37Okay, so here I will close this function with a tag summary line
04:44That a function is a house, a house, a house
04:49Now, the house inside the house, which you can understand the variable
04:54The things that are only for the house, it's only for the use of the house
04:58That the scope of the house is local
05:02Local people, local people, who can use them
05:07If you can access them, if you can access them, it's not possible
05:12Because a scope of the house is a scope
05:15And that scope is a scope, which is basically accessible
05:19For the house, it's not for the house
05:21So you can understand a function, a family example
05:23Now family inside the things, inside the people
05:27They are accessible, use them for the house
05:30It's not for the house, it's not for the house
05:33So these are scope, use them for the house
05:35So these are scope, the concept

Recommended