Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python Basics (2015) Shopping List App Shopping List Takeaways

Kevin Ohlsson
Kevin Ohlsson
4,559 Points

What does Kenneth mean with 'scope' and 'outside the scope of the script'?

Does he mean that functions are outside of the scope too? Why is the list 'outside of the scope'? It seems as if the script is in the middle of the functions and the 'meat' of the script?

Kevin are you talking about Kenneth's line about, "By using the mutable shopping list variable out in the global scope, or the outermost part of the script, we were able to update and access it in all of the other parts of our script." ?

Kevin Ohlsson
Kevin Ohlsson
4,559 Points

That's the line nekilof! I should've been more specific to begin with, i'll keep that in mind for when i post another question.

Jennifer answered my question, thanks for helping to clarify the question though!

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there again! I see you've run into the concept of scope. This is a tricky one. Kenneth sets up a list outside of the function we're going to use. Any variables we declare inside the function are only available while that function is executing. The second it ceases to execute, we no longer have access to those variables. This means that the variables inside the function are scoped to the function. However, if we declare a variable in the global scope (outside all functions), then this variable will be accessible everywhere in our code.

Hope this helps! :sparkles:

edited for additional information

As a side note, take a look at this quote from the video and see if it makes more sense with the above explanation.

By using the mutable shopping list variable out in the global scope, or the outermost part of the script, we were able to update and access it in all of the other parts of our script.

Kevin Ohlsson
Kevin Ohlsson
4,559 Points

Thank you (again!) Jennifer :)

Very clear. I understand that 'the scope' refers to the hierarchy of how code-blocks and pieces of code are placed in the program now, Is that correct?