Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing the modules in Python. OK, but why?

Tags:

python

I was going through a question in Checkio. And then i came across this.

import re,math
re > math # returns True
math > re # returns False

Can someone explain how Python compares between ANY two THINGS.

Does python does this thing by providing a hierarchy for modules. Furthermore,

re > 1 # return True # Ok, But Why?

I would really appreciate some deep explanations on these things!

like image 627
AnnShress Avatar asked Jan 16 '16 12:01

AnnShress


1 Answers

Everthing is an object. And modules are no exception. Therefore:

import re, math

print(id(re), id(math))
print(re > math)
print(id(re) > id(math))
print(re < math)
print(id(re) < id(math))
print(id(re), id(math))

In my case:

39785048 40578360
False
False
True
True
39785048 40578360

Your mileage may vary, because your ids will not be mine and therefore the comparison may be reversed in your case.

like image 88
mementum Avatar answered Oct 03 '22 06:10

mementum