4

I'm a programmer coming from Python background which typically uses lexical scoping and I want to understand dynamic scope. I've researched it on-line, still unclear to me. For example, I've read this page which made things a lot easier to me, especially the code snippets:

#In Dynamic scoping: 
 const int b = 5; 
 int foo() 
 { 
 int a = b + 5;
 return a;
 }
 int bar()
 {
 int b = 2;
 return foo();
 }
 int main()
 {
 foo(); // returns 10
 bar(); // returns 7
 return 0;
 }
#In lexical scoping: 
 const int b = 5; 
 int foo() 
 { 
 int a = b + 5;
 return a;
 }
 int bar()
 {
 int b = 2;
 return foo();
 }
 int main()
 {
 foo(); // returns 10
 bar(); // returns 10
 return 0;
 }

As I understand and as you can see, in the dynamic scoping, bar function returns 7 not 10, which means foo called the b variable which is defined within bar, in other words, foo did not looked for the b variable that's defined in top of the code (the first b variable) but rather it used the b variables that's defined in bar.

Q1: Hence, In dynamic scoping, the function that's being called within another function in this case foo called in bar first looks for variables in the caller function in this case bar and then it looks for top level variables ?

Q2: Could you explain dynamic scoping in Python code ?

Martijn Pieters
1.1m325 gold badges4.2k silver badges3.4k bronze badges
asked Sep 21, 2015 at 15:47
4
  • 4
    Python uses lexical scoping, there is no dynamic scoping. As such we cannot explain dynamic scoping in Python code. Commented Sep 21, 2015 at 15:49
  • 1
    The answer for Q1 is: yes. Commented Sep 21, 2015 at 15:51
  • @MartijnPieters about Q1 is this what all dynamic scoping boils down to or is there anything else ? Commented Sep 21, 2015 at 16:08
  • That's what it boils down to, yes. Also see Wikipedia. Commented Sep 21, 2015 at 16:13

1 Answer 1

10

There is no dynamic scoping in Python, unless you implement your own stack-based 'namespace'.

And yes, dynamic scoping is that simple; the value of a variable is determined by the closest calling namespace where a value was set. View the calls as a stack, and a value is looked up by searching the current call stack from top to bottom.

So for your dynamic example, the stack is first:

foo
main 
globals -> b = 5

finding b = 5 when searching through the stack, and then the stack changes to

foo
bar -> b = 2
main
globals -> b = 5

so b = 2 is found.

answered Sep 21, 2015 at 16:18
Sign up to request clarification or add additional context in comments.

11 Comments

So it's possible to find variables even in main function if for example b variable was assigned in main instead of bar ?
Yes, all scopes on the call stack are included in the search.
And if I called main or any other function outside functions, it'll only look for globals only, correct me if I'm wrong.
I see the point now why very limited people still relying on dynamic scoping as it's very complicated to comprehend, guess if you have about 100 functions in your program, it's really, really difficult to trace every single var, that being said, you need to build a tree of variable chain to understand where the problem is coming from in case of a bug
Aren't Python's context variables (3.7) just dynamically scoped variables?
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.