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.
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