Q51. Why should you avoid below import statement

from nameofmodule import *

 

Ans : Because, if you have created a variable with the same name as defined in import module as well, then it will overwrite your variable and it will be difficult to debug, what happened to the variable.

 

Q52. What is the best order to import the modules?

Ans : It is considered best practice, if you import the modules in following order

  • First import standard library module e.g. sys, os, re
  • Then third-party modules e.g. mx.DateTime
  • Then modules developed by you locally.  

 

Q53. What is the circular import and which form of circular import will not create a problem?

Ans : When you have import statement at the top and uses the “import nameofmodule” it is fine.

 

a.py

 

import b

b.py

 

import a

 

This is circular import and they will work fine, without any issue.

 

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

 

 

Q54. Which form of circular import will not work?

Ans : When you put circular import in the form of “from nameofmodule import something” and you will have circular dependencies than it will fail.

 

a.py

 

from b import x

b.py

 

from a import y

 

Here, module a is busy importing module b and in module b value y from module a is not yet available.  Hence, this kind of circular import should be avoided at the top level and needs to move import to function or class level.

 

Only move imports into a local scope, such as inside a function definition, if it’s necessary to solve a problem such as avoiding a circular import or are trying to reduce the initialization time of a module. This technique is especially helpful if many of the imports are unnecessary depending on how the program executes. You may also want to move imports into a function if the modules are only ever used in that function. Note that loading a module the first time may be expensive because of the one time initialization of the module, but loading a module multiple times is virtually free, costing only a couple of dictionary lookups. Even if the module name has gone out of scope, the module is probably available in sys.modules. 

 

Q55. When you define a function with default shared value like “def function_name(mydict={}):” what issue you will see? 

Ans: When you define a function as below

 

def fun1(my_dict={}):

                … do something…

                my_dict[key1]=”val1”

                return my_dict

 

Here, it is expected that every time you call the function fun1, it will create a new my_dict instance with no values. But this is not correct, because default values are created only once. And any subsequent call to that function will use the already created my_dict object.  

To avoid such scenario, you should have used None as a default value and inside the function you should check whether the value is None then create a new my_dict.

 

def fun1(my_dict=None):

                if my_dict is None:

                            my_dict={} #now it is a local dictionary object

 

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