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")
11 Answers
+ 1
Moe Williams
your code should be:
import math
import math as m
if math is m:
print ("True")
else:
print ("False")
+ 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
+ 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
+ 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.
+ 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")
+ 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
0
I am testing if math is imported as m.
0
Remember,I am using the is statement.
0
Can you break it down plz.
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))
0
Thanks