All language subtitles for 36. Why Do We Need Scope

af Afrikaans
ak Akan
sq Albanian
am Amharic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bem Bemba
bn Bengali
bh Bihari
bs Bosnian
br Breton
bg Bulgarian
km Cambodian
ca Catalan
ceb Cebuano
chr Cherokee
ny Chichewa
zh-CN Chinese (Simplified)
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English
eo Esperanto
et Estonian
ee Ewe
fo Faroese
tl Filipino
fi Finnish
fr French
fy Frisian
gaa Ga
gl Galician
ka Georgian
de German
el Greek
gn Guarani
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ia Interlingua
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
rw Kinyarwanda
rn Kirundi
kg Kongo
ko Korean
kri Krio (Sierra Leone)
ku Kurdish
ckb Kurdish (Soranรฎ)
ky Kyrgyz
lo Laothian
la Latin
lv Latvian
ln Lingala
lt Lithuanian
loz Lozi
lg Luganda
ach Luo
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mfe Mauritian Creole
mo Moldavian
mn Mongolian
my Myanmar (Burmese)
sr-ME Montenegrin
ne Nepali
pcm Nigerian Pidgin
nso Northern Sotho
no Norwegian
nn Norwegian (Nynorsk)
oc Occitan
or Oriya
om Oromo
ps Pashto
fa Persian
pl Polish
pt-BR Portuguese (Brazil)
pt Portuguese (Portugal)
pa Punjabi
qu Quechua
ro Romanian
rm Romansh
nyn Runyakitara
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
sh Serbo-Croatian
st Sesotho
tn Setswana
crs Seychellois Creole
sn Shona
sd Sindhi
si Sinhalese
sk Slovak
sl Slovenian
so Somali
es Spanish
es-419 Spanish (Latin American)
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
tt Tatar
te Telugu
th Thai
ti Tigrinya
to Tonga
lua Tshiluba
tum Tumbuka
tr Turkish
tk Turkmen
tw Twi
ug Uighur
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
wo Wolof
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu

Original subtitles

I want to address a question that you may or may not have but it's something that's taken me years to

understand and that is why not just have everything as global variables.

I mean all this confusion of who has access to who.

How easy would it be if everything was just on the main page.

All our information all the data on our global scope so that everything has access to everything.

Wouldn't that be easier and you'd be kind of right.

I mean that would make all this headache go away.

Right.

But you have to remember that machines don't have infinite power don't have infinite CPR you don't have

infinite memory they all have limited resources.

And as programmers we have to be conscious of what resources we use because sometimes that can cost

us money.

Sometimes that can crash our computers and scope is a great demonstration of this.

For example this code right here when this function is run we're creating technically just one location

in memory for the x variable.

So we have that bookshelf in our computer that is X. That's pointing to local when we actually call

this function.

And then when we say non-local here Well we're saying just don't create another bookshelf for us.

Just use the one that we already have and assign it non-local if we didn't have this line by the time

we get to line seven.

We've placed in memory x equals two local and x equals two non-local.

So we have two locations now in memory.

Now this isn't a big deal because well in this day and age we do have a lot of memory but as programs

get larger and larger this does become a bit of a problem.

OK.

But what about a function.

I mean we learned that functions allow us to not repeat ourselves and being able to call out our multiple

times.

But another good use of functions is that once we call this function and all of this is done the computer

and the Python interpreter specifically destroys all this memory that is once we finish with the outer

function.

I can't really call print x here.

It's going to give me an error.

It's going to say I have no idea what X is and why is that it's because after we call this function

the python will be called the garbage collector is going to say hey it looks like we're done with this

function.

And I see that this x variable well in this x variable we're not gonna use because we're done with this

function.

So I'm going to collect that garbage and then just throw it out.

So I'm going to empty that memory cupboard so that other resources or other programs can use that.

And that is a really nice feature where python just automatically removes these for you so that you

don't clog up the computer's memory and that's why scope is useful.

We don't have to think about it in such detail like I've mentioned it but it's nice to know that it's

there so that your programs don't hog up a lot of memory and they can run efficiently.

This is a bit of an advance topic but I did want to include it in here so that you can think about why

programs are designed the way they are.

All right let's stop talking now.

I'll see you in the next video by.

Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.