Why: Python Scopes, Namespaces & Private Variables? (W/ Mnemonics)

in dtube •  2 years ago 

Get The Full "Learn Python 🐍" Online Course:

The following video is from an online educational course produced by Mnemonic Academy that teaches the basics of Python programming using mnemonics, metaphors and the science of learning titled "Learn Python: Like a Bee Student", taught by instructor Dylan Jorgensen

What is scope? Variable scope?
When a variable is used in a code block, that variable may or may not have permission to be used outside of the code block.
So scope identifies the boundaries that a variable can found within.
Scope errors cause a "NameError"

What defines the rules of Scope? LEGB rules?
There is a concise set of rules that can be summarized in the acronym LEGB.
L. is Local — Variables assigned in any way within a function, and not declared global in that function.
E. is Enclosing — Variables in the local scope of any and all enclosing nesting functions, from inner to outer.
G. is Global (module) — Variables assigned at the top-level of a module file, or declared global.
B. is Built-in (Python) — Variables preassigned in the built-in names module. Examples are: open, range & SyntaxError.

What's the difference between a variable's scope and a variable's lifetime?
Because all variables are going to go through a process of mutability and evolving over time, the variable's lifetime is a reference to points in the lifetime.
Like in our penguin example this would be the difference between a baby penguin and a teenager penguin. Independent of which exhibit the penguin is found in, inside of our zoo.

"Names" and "binding" vs. "variables" and "assignment"?
In this course we are going to stick to variables and assignments. But some day you might run into the coding equivalent of the grammar police, who will lecture you on how it's better to think of variables as names; and assignments as bindings.

What is a namespace? Variables space?
A namespace is a way to see what names/variables we have in our scope.
A namespace, is a fenced in area with a list of all available names in our current scope.
A name can also map to a function or any other python object. So importing a module or function can bring more names along with it so your name spaces holds the functions, classes and variables (vars) your module has access to.
In Python, each package, module, class, function and method function owns a "namespace" in which variable names are resolved.
There's also a global namespace that's used if the name isn't in the local namespace.
To use libraries you have to load them into your namespace.

How do I see what my namespaces are?
We can use a python function called "Locals()" to print our local variables.
Or we can use a python function called "Globals()" to print global variables.
They both return a dictionary containing all the variable names Python knows about.

What are public, private and protected variables? Related to classes?
For now think of classes as areas of the zoo (e.g. like animals from Africa).
Public variables, are variables that are visible to all classes.
Private variables, are variables that are visible only to the class to which they belong.
Protected variables, are variables that are visible only to the class to which they belong, and its subclasses.

Single Underscore?
Names in a class, with a leading underscore are simply to indicate to other programmers that the attribute or method is intended to be private. However, nothing special is done with the name itself.

Double Underscore (Name Mangling)?
Identifiers with double_underscores get their underscores replaced with classnames.
Methods that start like __cowboyhat (at least two leading underscores, at most one trailing underscore) is textually replaced with _classname__cowboyhat.

Interested in Learning More About Online Courses That Teach Using Mnemonics, Metaphors and the Science of Learning?

Checkout All Our Mnemonic Academy 🐝 Online Courses:

Subscribe To Our Mnemonic Academy 🐝 YouTube Channel:

Like Our Mnemonic Academy 🐝 Facebook Page:

► Watch on DTube
► Watch Source (IPFS)
Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Upvoted by @toppeople , Upvote my post . Thank you