I have just seen the following Python code:
# Static Binding
s = 2
def f(x):
return x * s
def g():
s = 3
if f(3)==9:
print “Result “, f(3)
print “Dynamic Binding”
else:
print “Result “, f(3)
print “Static Binding”
if __name__ == “__main__”:
print “f(3): ” , f(3)
print “g(): “, g()
Output:
f(3): 6
g(): Result 6
Static Binding
# Dynamic Binding
s = 2
def f(x):
return x * s
def g():
global s
s = 3
if f(3)==9:
print “Result “, f(3)
print “Dynamic Binding”
else:
print “Result “, f(3)
print “Static Binding”
if __name__ == “__main__”:
print “f(3): ” , f(3)
print “g(): “, g()
Output:
f(3): 6
g(): Result 9
Dynamic Binding
The author of this code says that this is an example of Dynamic Binding, but I seen it more like an example of how to use a global variable in Python. For what I know, for example in Java, dynamic binding is when an object grabs some methods according to what type of class do they belong to it, but here I do not see it. Am I missing something?
Thanks