0

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

Layla
  • 5,234
  • 15
  • 51
  • 66
  • 2
    That's not dynamic vs. static binding. I don't have a name for it, but that one isn't correct. – Ignacio Vazquez-Abrams Sep 14 '14 at 14:08
  • [Python Scopes and Namespaces](https://docs.python.org/2.7/tutorial/classes.html#python-scopes-and-namespaces) and [Naming and Binding](https://docs.python.org/2.7/reference/executionmodel.html#naming-and-binding) - neither mentions *dynamic* or *static*. Looks like an attempt to bleed part of one language's paradigm into another. – wwii Sep 14 '14 at 15:12
  • The author is confused. That's an example of local verses global variables as you say. Everything in python is what java would call "dynamic binding" because everything can be overridden. Not surprising that a language without a compiler wouldn't have static binding. – tdelaney Sep 14 '14 at 15:21
  • @tdelaney: Python has a compiler, it just doesn't result in executables. – Ignacio Vazquez-Abrams Sep 16 '14 at 08:27
  • @IgnacioVazquez-Abrams I guess we could debate about how compilerish python is (it generates byte codes) but doesn't do static binding. – tdelaney Sep 16 '14 at 15:17

0 Answers0