FAQ: Python Namespaces - Enclosing Namespace

This community-built FAQ covers the “Enclosing Namespace” exercise from the lesson “Python Namespaces”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn Intermediate Python 3

FAQs on the exercise Enclosing Namespace

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in Language Help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to Language Help and Tips and Resources. If you are wanting feedback or inspiration for a project, check out Projects.

Looking for motivation to keep learning? Join our wider discussions in Community

Learn more about how to use this guide.

Found a bug? Report it online, or post in Bug Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Is it meant to be functon or function in the description.

1 Like

I’d assume ‘function’ but at least it’s consistently misspelled so the identifier is still valid. It’s been raised as a bug anyway, ta.

How do I access the parents namespace from within an enclosed function?

I noticed the locals() only returns the variables from the function itself, globals() returns the same global namespace as when called from the main code. Hence my question, how can I get the object’s parent namespace?

2 Likes

I don’t think there’s a standard route to access all the names from the enclosing (function) scope. Don’t forget that the effective ‘namespace’ of a function only really has a meaning during a function call and the actual objects are forgotten afterwards. Closures are a very special case where an additional reference to an object from the outer scope is effectively captured by an inner function.

Therefore inside an inner function that used names from the enclosing scope the only enclosing scope names you’d have direct access to are those that were used when the (inner) function was first defined (so you already have references for them anyway).

It’s a bit of a complex implementation detail that you probably don’t need to worry about. In short, there is no simple way to do this.

2 Likes

It might be able to do with inspect module (which I learned today).

import inspect global_variable = 'global' def outer_function(): outer_value = "outer" def inner_function(): inner_value = "inner" def inner_nested_function(): nested_value = 'nested' inner_nested_frame = inspect.currentframe() inner_frame = inner_nested_frame.f_back outer_frame = inner_frame.f_back global_frame = outer_frame.f_back print(inner_nested_frame.f_locals) print(inner_frame.f_locals) print(outer_frame.f_locals) print(global_frame.f_locals) inner_nested_function() inner_function() outer_function()
1 Like