Python Global Variables (☠ Don't Use them!) #14

preview_player
Показать описание
Scope refers to the availability of variables and functions in certain parts of the code.

In this video, we will learn about the concept of global and local variables in Python. Local variables are variables declared inside the function body and they cannot be accessed from outside the function. Global variables are the variables declared outside of a function or in the global scope.

~

Watch our videos and revise them with our Python App!

Timestamps:
0:00 Intro
0:23 Local Variables
2:28 Global Variables
5:16 Recap

Find Programiz elsewhere:
-------------------------------------------------

Python Global Variables (☠ Don't Use them!) #14

#python #learnprogramming #programiz #localvariables #globalvariables
Рекомендации по теме
Комментарии
Автор

🔥Finding it Damn Hard to Understand Python?
Learn to code—the right way—with interactive lessons, quizzes & challenges. Build a strong programming base; it's IMPORTANT!

programizstudios
Автор

Dear Programiz Team, the GREAT one, kindly receive my sincere thanks. Your talent of presentation has simplified my learning very much. Thank you indeed.

kakairecharles
Автор

one of best and qualityfull videos on python, thanks for this...

priyanshusingh
Автор

Love the videos. Still helpful today. I actually agree with "don't use them", unless there is NO OTHER WAY to do it.

Python needs "True Globals", which can be defined OUTSIDE functions, as BEING a global. EG, so if it is within ANY function, it is NOT treated as a LOCAL. Because if it was "truly global", then myVal outside a def or class, would be the same value within the def or class... we would HAVE to define locals so a "global" didn't conflict. The fact that we have to TELL the def or class to use an "outside-scope value", is the actual definition of NOT being global.

In an attempt to make it easy, they made it harder and confusing instead. "Global" should have been named "Parent" or "Scoped", not "Global". Because it ISN'T global, it's just "pre-scoped", as the property of the "parent". The "child" being the def/class or whatever nested/isolated scope they create next. :P

What everyone wanted/expects... (Note, this clearly doesn't work. Because defining a "global" outside a function is literally redundant, because all functions are global, outside. Which is not actually true, if you HAVE to tell a function to use it. It's just another form of a local value.) So, back to what we all wanted...


global = self <- prepends this file name as the owner of the global (myFile.aValue) so imports can use it
global aValue = 'ava' <- should be 'ava' ANYWHERE that (aValue) is used, including any imports, if desired (noted above)
bValue = 'bob' <- only 'bob' if it is used in this file, EG: a parent local, just as it is now
cValue = 'cam' <- same as bValue, a parent/file local.

def myFunc():
-> aValue = 'sue' <- Would change 'ava' to 'sue' globally, because the program "claimed" rights to this value, globally
-> bValue = 'joe' <- Would be LOCAL-HERE, aka: child-value to the file, the function being the parent owner. Addressable, if not a class, as myFunc.bValue in the parent file, or myFile.myFunc.bValue from any imported file, if this file was scoped with the (global = self) function that doesn't exist. :P
-> global cValue
- cValue = 'pam' <- Would act normal, changing the UNDEFINED GLOBAL value in the parent/file, outside the function

Why would we want this, or expect this to be a possibility? Because then we don't have to type, what should be default, in EVERY FUNCTION. Saving hours of headaches, as we try to figure out why something isn't working because we forgot to define a specific global within some obscure function, so now it's using a local value.

As opposed to creating a function, testing it, and instantly realizing, OOPS, "I used a defined global here!" Which is something you can CLEARLY see and any IDE can indicate, since it's ALREADY DEFINED AS GLOBAL. Which is why EVERY programming language, that I know of, has things like "declarations" and "constants" and other things in place. Purposely excluding them was the WORST decision that python ever made. Other than demanding that we "define" them within a function, as "global". It's as redundant as defining self to anything, when everything inside should be 'self', and actually is... which is WHY it is local. (In a function or a class).

Refering to this stupid line of code...
class Person:
-> def __init__(self, name, age):
-> -> self.name = 'bob'
Which should be something similarly logical like this...

class Person(name, age):
-> name = 'bob'

Not sure why they went all-out and purposely made classes annoyingly complex. That should be Person.name, just as it should be within a 'def myFunc():' The word 'class' being all that the program needs to identify it as a class.

JD_Mortal
Автор

Helped alot with a problem I spend hours looking for and tried to fix! Thanks so much!

skott
Автор

One video about *args and **kwargs is also required.

ronaksuthar
Автор

Big support from Israel 👏
Your site and app have great content and design that speed up my learning in Java and Python . I am sharing your articles with colleagues on a daily basis.

jonathan
Автор

Finally I have found easy and straight way to learn programming

ganeshkolla
Автор

This is a start, I am trying to learn python so I can work on small projects. Hopefully by the time I finish this series I will be able to do that and more.

LordInsider
Автор

Best explanation and code examples easy to understand everyone else was so confusing...

deaththeworld
Автор

My LEGEND. You just made my day with that global knowledge

rutgervanhuyssteen
Автор

Sir more video!

And Sir, Very Very thanks for this python series.
😊

abhinav
Автор

Sir how can we make a good calculator using python?

learngermanwithvardaanagga
Автор

sir its a very good video but i have seen all your videos can you put your new videos quickly so that I
can continue the syllabus and sir please tell us when we will be able to actually use all the skills learned from you to create website

kartikeshwarhingole
Автор

Sir for the first time my functions doubt is cleared.
But the 'init' in function is not explained yet.

himankaghosh
Автор

Thank you very much for your excellent clarification.

sasindukasunsi
Автор

How can I access your full video on functions

balougunabiodun
Автор

Sir in the local variable example (add_numbers) what is the output comes for local variable

monishav.sc
Автор

Sir, I do not understand anything after functions statments I use to study in app also pls help I m revising this course again and again 🙏

Rupank_Sahu
Автор

let's say i put a global function inside a defined function, how can i end/terminate the global function in the defined function itself?

mcronut