Computer Science
Create module tempConversion.py as given in Fig. 4.2 in the chapter. If you invoke the module with two different types of import statements, how would the function call statement for imported module's functions be affected ?

Python Libraries
6 Likes
Answer
If we invoke the tempConversion module with two different types of import statements (import tempConversion
and from tempConversion import *
), the way we call the module's functions will be affected as follows:
1. import tempConversion
— This imports the entire module in a new namespace setup with the same name as that of the module tempConversion. To call the module's functions, we need to use the dot notation tempConversion.<function_name>
.
For example:
import tempConversion
tempConversion.to_centigrade(32)
tempConversion.to_fahrenheit(0)
2. from tempConversion import *
— This imports all objects (functions, variables, etc.) from the module into the current namespace. This approach imports all objects directly into the current namespace, so we can call the module's functions without using the module name.
For example:
from tempConversion import *
to_centigrade(32)
to_fahrenheit(0)
Answered By
2 Likes
Related Questions
What do you understand by standard library of Python ?
Explain the difference between
import <module>
andfrom <module> import
statements, with examples.A function checkMain() defined in module Allchecks.py is being used in two different programs. In program 1 as
Allchecks.checkMain(3, 'A')
and in program 2 ascheckMain(4, 'Z')
. Why are these two function-call statements different from one another when the function being invoked is just the same ?Given below is semi-complete code of a module basic.py :
# """...............""" def square(x): """...............""" return mul(x, x) ...............mul(x, y): """...............""" return x * y def div(x, y): """...............""" return float(x)/y ...............fdiv(x, y)............... """...............""" ...............x//y def floordiv(x, y)............... ...............fdiv(x, y)
Complete the code. Save it as a module.