You will execute the python program provided below to determine if: • Dynamic or
ID: 3758289 • Letter: Y
Question
You will execute the python program provided below to determine if:
• Dynamic or Static scoping is being used,
• Dynamic or Static type binding is being used, and
• Dynamic or Static type checking is being used.
Justify your answer based on the results of executing the program:
def x():
# Dynamic or Static scoping?
stack=[1]*5
def z():
print(stack[1])
def y():
stack=[2]*5
z()
y()
# Dynamic or Static type binding?
a = 5
print(a)
a='abc'
print(a)
# Dynamic or Static type checking?
a = 5
b = 'abc'
print (a+b)
x()
Explanation / Answer
Dynamic Scoping language :
Strictly speakingand in practice for most programming languages, "part of a program" refers to "portion of the source code (area of text)", and is known as lexical scope. In some languages, however, "part of a program" refers to "portion of run time (time period during execution)", and is known as dynamic scope.
Lexical scope vs. dynamic scope :
A fundamental distinction in scoping is what "part of a program" means. In languages with lexical scope,name resolution depends on the location in the source code and the lexical context, which is defined by where the named variable or function is defined. In contrast, in languages with dynamic scope the name resolution depends upon the program state when the name is encountered which is determined by the execution context or calling context. In practice, with lexical scope a variable's definition is resolved by searching its containing block or function, then if that fails searching the outer containing block, and so on, whereas with dynamic scope the calling function is searched, then the function which called that calling function, and so on.Lexical resolution can be determined at compile time, and is also known as early binding, while dynamic resolution can in general only be determined at run time, and thus is known as late binding.
Example :
Staic Type Binding :
This is a minimal problem, because an experient developer will never do this, but is still possible and in languages with static type binding is not.
You have to keep in mind that we are talking about concepts, and implementations of those concepts are evolving together with computer science, so probably some Advantages/Disadvantages can be minimized on nowadays.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.