Python Forum
Namespace and scope difference
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Namespace and scope difference
#6
In order to fully understand namespace and scope you should familiarise yourself with closure as well. Python supports closures: functions that refer to variables from the scope in which they were defined.

ichabod801 used terms 'typically' and 'without some tricks' for a reason. There is much subtlety and in order to understand you must look at the whole picture.

There are behaviours which seem illogical at first but actually make sense if you think about it. Just to illustrate:

>>> def inner():
...     y = 1
...     def innermost():
...         y = 2
...     innermost()
...     print(y)
... 
>>> inner()
1
>>> def inner():
...     y = [1]
...     def innermost():
...         y[0] = 2
...     innermost()
...     print(y)
... 
>>> inner()
[2]
EDIT:

You should also understand difference between reference and assignment:

When you reference a variable in an expression, the Python interpreter will traverse the scope to resolve the reference in following order:

- current function’s scope
- any enclosing scopes (like containing functions)
- scope of the module that contains the code (global scope)
- built-in scope (that contains functions like int and abs)

If Python doesn't find defined variable with the referenced name, then a NameError exception is raised.

Assigning a value to a variable works differently. If the variable is already defined in the current scope, then it will just take on the new value. If the variable doesn’t exist in the current scope, then Python treats the assignment as a variable definition. The scope of the newly defined variable is the function that contains the assignment.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Messages In This Thread
Namespace and scope difference - by Uchikago - Jul-03-2019, 12:57 PM
RE: Namespace and scope - by ichabod801 - Jul-03-2019, 01:09 PM
RE: Namespace and scope difference - by perfringo - Jul-03-2019, 01:14 PM
RE: Namespace and scope difference - by Uchikago - Jul-03-2019, 01:32 PM
RE: Namespace and scope difference - by ichabod801 - Jul-03-2019, 01:40 PM
RE: Namespace and scope difference - by perfringo - Jul-03-2019, 02:06 PM
RE: Namespace and scope difference - by ichabod801 - Jul-03-2019, 02:25 PM
RE: Namespace and scope difference - by Uchikago - Jul-03-2019, 03:17 PM
RE: Namespace and scope difference - by ichabod801 - Jul-03-2019, 03:31 PM
RE: Namespace and scope difference - by Uchikago - Jul-03-2019, 03:36 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to create a variable only for use inside the scope of a while loop? Radical 10 1,980 Nov-07-2023, 09:49 AM
Last Post: buran
  Library scope mike_zah 2 892 Feb-23-2023, 12:20 AM
Last Post: mike_zah
  Scope of variable confusion Mark17 10 2,984 Feb-24-2022, 06:03 PM
Last Post: deanhystad
  Variable scope issue melvin13 2 1,631 Nov-29-2021, 08:26 PM
Last Post: melvin13
  'namespace' shorthand for function arguments? shadowphile 5 2,674 Aug-11-2021, 09:02 PM
Last Post: shadowphile
  Variable scope - "global x" didn't work... ptrivino 5 3,133 Dec-28-2020, 04:52 PM
Last Post: ptrivino
  Python Closures and Scope muzikman 2 1,899 Dec-14-2020, 11:21 PM
Last Post: muzikman
  [PyKML] Loop through all Placemarks; Remove namespace Winfried 2 3,499 Aug-28-2020, 09:24 AM
Last Post: Winfried
  Block of code, scope of variables and surprising exception arbiel 8 3,511 Apr-06-2020, 07:57 PM
Last Post: arbiel
  Beginner question: lxml's findall in an xml namespace aecklers 0 2,963 Jan-22-2020, 10:53 AM
Last Post: aecklers

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020