Jay Taylor's notes
back to listing indexPython nested functions variable scoping
[web search]
I've read almost all the other questions about the topic, but my code still doesn't work. I think I'm missing something about python variable scope. Here is my code:
And I get
I know the problem is on the Can someone explain to me what I'm missing about python variable scope? |
|||||||||||||||
|
|||||||||||||||
When I run your code I get this error:
This problem is caused by this line:
The documentation about Scopes and Namespaces says this:
So since the line is effectively saying:
it creates |
||||
Here's an illustration that gets to the essence of David's answer.
With the statement But if you uncomment that line, on the line
It seems mysterious that the presence of |
|||||||||||||||||||||||||||
|
In Python 3, you can use the |
|||||||||
|
Rather than declaring a special object or map or array, one can also use a function attribute. This makes the scoping of the variable really clear.
Of course this attribute belongs to the function (defintion), and not to the function call. So one must be mindful of threading and recursion. |
|||||||||
|
This is a variation of redman's solution, but using a proper namespace instead of an array to encapsulate the variable:
I'm not sure if using a class object this way is considered an ugly hack or a proper coding technique in the python community, but it works fine in python 2.x and 3.x (tested with 2.7.3 and 3.2.3). I'm also unsure about the run-time efficiency of this solution. |
|||||||||
|
You probably have gotten the answer to your question. But i wanted to indicate a way i ussually get around this and that is by using lists. For instance, if i want to do this:
I would instead do this:
This way X is never a local variable |
|||||||||||||||||||||||||||
|
More from a philosophical point of view, one answer might be "if you're having namespace problems, give it a namespace of its very own!" Providing it in its own class not only allows you to encapsulate the problem but also makes testing easier, eliminates those pesky globals, and reduces the need to shovel variables around between various top-level functions (doubtless there'll be more than just Preserving the OP's code to focus on the essential change,
As a PS, one hack which is a variant on the list idea in another answer, but perhaps clearer,
|
|||
as you see, total is in the local scope of the main function, but it's not in the local scope of recurse (obviously) but neither it is in the global scope, 'cause it's defined only in the local scope of get_order_total |
|||
While I used to use @redman's list-based approach, it's not optimal in terms of readability. Here is a modified @Hans' approach, except I use an attribute of the inner function, rather than the outer. This should be more compatible with recursion, and maybe even multithreading:
This prints:
If I
So, indeed, it seems better to make them the inner function's attributes. Also, it seems sensible in terms of readability: because then the variable conceptually relates to the inner function, and this notation reminds the reader that the variable is shared between the scopes of the inner and the outer functions. A slight downside for the readability is that the |
|||
Your Answer
Not the answer you're looking for? Browse other questions tagged python variables scope or ask your own question.
asked |
5 years ago |
viewed |
21939 times |
active |
Get the weekly newsletter! In it, you'll get:
- The week's top questions and answers
- Important community announcements
- Questions that need answers
see an example newsletter
Linked
Related
Hot Network Questions
- New OS for old PCs
- How did Ser Jorah do it this time?
- In Unreal, what are the differences between C++'s float and FFloat32?
- How to make tied knots in blender?
- Torque wrench or impact gun on large bolts
- "She speaks an impeccable English" vs "She speaks impeccable English"
- What recourse should I offer my client if we lose user data?
- How do I protect my shop from teleporters?
- From below to time of need
- CPU issues caused by process builder and workflows has really gotten bad in last 3 releases
- What are the shades of green?
- Is the “Coding Method” a reliable method to learn vocabulary?
- Why are PDF in pdfinfo keywods run together
- On machines, what is the weight listed?
- Is heap a table without indexes at all or without a clustered index?
- How to motivate youngsters/millennials to save money
- How can an electron shell hold more than two electrons?
- Why do modern computer cases still have USB 2.0 ports?
- Is PHP loadXML vulnerable to XXE attack (and to other attacks)? Is there a list of vulnerable functions?
- I last billions of years or a couple of milliseconds
- Can I reserve a beach spot by leaving an umbrella and lounge chair overnight (Italy)?
- What is the meaning of なめた口?
- Is “Untap” an equivalent of “Unleash?”
- Breath mediation during daily acvities
Technology | Life / Arts | Culture / Recreation | Science | Other | ||
---|---|---|---|---|---|---|