Q46. What is UnboundLocalError and why you get it?

Ans: UnboundLocalError is a error when , you try to assign a local variable which is not initialized.

 

Let’s check following steps

 

name='Amit'

def fullname():

    print name

 

fullname() # call the function, it will work fine.

   

 

Now define the function as below

name='Amit'

def fullname():

    print name

    name= name+'Khanna'

 

fullname() # call the function, it will give error ‘UnboundLocalError’

 

 

Reason: This is because when you make an assignment to a variable in a scope, that variable becomes local to that scope and shadows any similarly named variable in the outer scope. Since the last statement in fullname assigns a new value to name, the compiler recognizes it as a local variable. Consequently when the earlier print name attempts to print the uninitialized local variable and an error results.

Learn Python in Less than 8 Hours sitting at Home/@Desk

 

 

 

 

Q47. How can be avoided ‘UnboundLocalError’ with the global variable?

Ans: You have to mark the variable global explicitly

 

def fullname():

    global name

    print name

    name= name+'Khanna'

    print name

   

fullname()

 

Q48. I am still confused with the local and global variables of Python?

Ans : If you have defined variable outside the function and only referencing them inside the function is considered global variable. But if you try to assign them with same or new value then it will be considered a local variable and local variable must be initialized first to use it else you will get UnboundLocalError’

 

 

Q49. You are having below code with the Lambda

 

doubled= []

for v1 in range(5) :

                doubled.append(lambda:2*v1)

print(doubled[0](), doubled[1](), doubled[2](), doubled[3]() , doubled[4]())

 

Why all the elements in doubled list are 10

 

Ans: Because v1 is not a local variable and it is defined in the outer scope and will be accessed in lambda function. Hence, when the loop ends, value of the v1 variable will be 4. Hence, all the values in the list will become 8.

 

To avoid this, you have to define a new variable that is local to the lambda as below

doubled= []

for v1 in range(5) :

                doubled.append(lambda x=v1:2*x)

print(doubled[0](), doubled[1](), doubled[2](), doubled[3]() , doubled[4]())

 

Now, here x is local variable to lambda, which holds the current value and then doubled and then appended to list.

 

 

 

 

Q50. How to make a variable shared across all the modules in your application and it should be global?

Ans: If you want to make a variable global across all the modules in your application than you have to define a new module usually config.py and then define that global variable in that module.

 

Import the config.py module in each of your application module. And as you know, only one instance of a particular module will be created. Hence, any change you make to global variable in any module will be reflected in each module. See below example with three different module files

 

 

config.py

 

price=1000 # Default price for the course will be 1000INR

hadoop.py

 

import config

config.price=2000 #Increase the price to 2000INR and same will be reflected in each module

course_detail.py

 

import config

import hadoop

print(config.price)

 

 

Why Dont you prepare for Python Certifications and Interview Questions with 250+ Questions and Answer : Check Here

Real Exam Number of Questions: 70 Questions
Real Exam Pass Score: 70%
Time Allotted: 90 minutes to complete exam