in Technology by
What is Scope Resolution in Python?

1 Answer

0 votes
by

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)

print(temp) 	 # output => 10
func() 		 # output => 20
print(temp) 	 # output => 10

This behaviour can be overriden using the global keyword inside the function, as shown in the following example:

temp = 10 	 # global-scope variable

def func():
      global temp
     

Related questions

0 votes
0 votes
    Which Azure networking component is the core unit, from which administrators can have full control over IP address ... . Load Balancers 3. Routing Tables 4. Local Network...
asked Sep 22, 2021 in Technology by JackTerrance
+1 vote
    Which Azure networking component is the core unit from which administrators can have full control over IP address ... , name resolution, security settings, and routing rules?...
asked Oct 20, 2020 in Technology by JackTerrance
+1 vote
    Which Azure networking component is the core unit from which administrators can have full control over IP address ... , name resolution, security settings, and routing rules?...
asked Oct 20, 2020 in Technology by JackTerrance
0 votes
    What is Coreference Resolution? (a) Anaphora Resolution (b) Given a sentence or larger chunk of text, determine which ... d) None of the mentioned Please answer the above question....
asked Oct 22, 2022 in Education by JackTerrance
0 votes
    What is Coreference Resolution? (a) Anaphora Resolution (b) Given a sentence or larger chunk of text, determine which ... d) None of the mentioned Please answer the above question....
asked Oct 2, 2022 in Education by JackTerrance
0 votes
    Explain with reasons whether the following statements are true or false: A resolution can be passed even if China exercises its veto power. Please answer the above question....
asked Aug 14, 2022 in Education by JackTerrance
0 votes
    I have three text fields and submit button on a browser page.. Lets say A, B, C, and submit ( ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 17, 2022 in Education by JackTerrance
0 votes
    I'm having some trouble understanding the changes made to the coref resolver in the last version of the ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    I am porting a game, that was originally written for the Win32 API, to Linux (well, porting the ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jan 13, 2022 in Education by JackTerrance
0 votes
    Fill in the Blanks. (5) A. is a type of scanner that is used to capture the highest resolution from an image. Select the correct answer from above options...
asked Dec 24, 2021 in Education by JackTerrance
0 votes
    What is the dependency scope? Name all the dependency scope....
asked May 4, 2021 in Technology by Editorial Staff
0 votes
    Which of the following regarding scope is true? 1. Variables that have a local scope are only visible in the ... use in your program have to be declared as global variables...
asked Feb 25, 2021 in Technology by JackTerrance
0 votes
    What is Usability Testing and what is its scope?...
asked Feb 8, 2021 in Technology by JackTerrance
...