What Is Scope Resolution in Python?
What Is Scope Resolution in Python?
Sometimes objects within the same scope have the same name but function differently. In
such cases, scope resolution comes into play in Python automatically. A few examples of
such behaviour are:
• Python modules namely 'math' and 'cmath' have a lot of functions that are common to both of
them - log10(), acos(), exp() etc. To resolve this amiguity, it is necessary to prefix them with
their respective module, like math.exp() and cmath.exp().
• Consider the code below, an object temp has been initialized to 10 globally and then to 20 on
function call. However, the function call didn't change the value of the temp globally. Here, we
can observe that Python draws a clear line between global and local variables treating both
their namespaces as separate identities.
temp = 10 # global-scope variable
def func():
temp = 20 # local-scope variable
print(temp)
def func():
global temp
temp = 20 # local-scope variable
print(temp)
@names_decorator
def say_hello(name1, name2):
return 'Hello ' + name1 + '! Hello ' + name2 + '!'