0

Plz help me with the is statement

Do you put it like this: import math as m if math is imported as m: print ("True") else: print ("False")

16th Jul 2022, 6:35 PM
Moe Williams
Moe Williams - avatar
11 Answers
+ 1
Moe Williams your code should be: import math import math as m if math is m: print ("True") else: print ("False")
24th Jul 2022, 3:58 AM
Bob_Li
Bob_Li - avatar
+ 5
I don't quite understand what your goal is? You import math with the alias m and then want to know what m is right in the next line? m.__name__ will return the name math
16th Jul 2022, 6:41 PM
Lisa
Lisa - avatar
+ 4
Remember that your code is not doing what you think it is. We gave you examples how to check what "m" is and how to test if the module was successfully imported Also, your code does not have any indentation
17th Jul 2022, 5:26 PM
Lisa
Lisa - avatar
+ 3
'is' keyword is used to check if two objects share the same memory location. Try this: import math print('math id =', id(math)) import math as m print('m id = ', id(m)) print(m is math) print(math is m) but the id() is the same because as Lisa said, what is happening is python is creating an m.__name__:"math" and using that under the hood. It points m to the same id as math. Math is not actually imported as m, rather, the variable name m is given the same id as math.
18th Jul 2022, 12:54 AM
Bob_Li
Bob_Li - avatar
+ 1
Moe Williams if the import fails, then it will cause an exception. Otherwise you can trust that m will be a good alias of math. You could trap the error in try/except block. Example: try: import grok as g print("grok import succeeded") except: print("grok not imported")
16th Jul 2022, 7:21 PM
Brian
Brian - avatar
+ 1
Moe Williams for now, you will have to write code according to Python's rules. If you feel that is wrong, here is where you can find information on proposing changes: https://mail.python.org/pipermail/python-checkins/2011-January/101423.html
17th Jul 2022, 5:35 PM
Brian
Brian - avatar
0
I am testing if math is imported as m.
16th Jul 2022, 6:45 PM
Moe Williams
Moe Williams - avatar
0
Remember,I am using the is statement.
17th Jul 2022, 5:14 PM
Moe Williams
Moe Williams - avatar
0
Can you break it down plz.
23rd Jul 2022, 7:42 PM
Moe Williams
Moe Williams - avatar
0
import math print("id(math) =", id(math)) import math as m print("id(m) =", id(m)) x = math print("id(x) =", id(x)) y = m print("id(y) =", id(y)) #if id() are the same, 'is' returns True print("m is math =", m is math) print("m is y =", m is y) print("math is x =", math is x) #you can even do print(y.sqrt(4))
24th Jul 2022, 3:54 AM
Bob_Li
Bob_Li - avatar
0
Thanks
24th Jul 2022, 4:42 PM
Moe Williams
Moe Williams - avatar