In the below code snippet, two objects named div
are created at lines 1 and 2.
How does python differentiate between the two
div
objects created under the same scope?When
id()
is applied on both objects, two different addresses are shown for the similar named objects. Why is this so?
def div(a,b):
return a/b
print(id(div)) # id = 199......1640 ################################ line 1
def smart_div(func):
def inner(a,b):
if a<b:
a,b=b,a
return func(a,b)
return inner
a = int(input("Enter 1st: "))
b = int(input("Enter 2nd: "))
div = smart_div(div)
print(id(div)) # id = 199......3224 ############################# line 2
print(div(a,b))
In legacy languages like C, one can't create two variables with the same name under same scope. But, in python this rule does not seem to apply.