I am reading this piece of code by Alex Martelli mentioned in this question. I understand that sys.modules[__name__]
tells you what module you are currently at, but this line of code at the end of his constant.py
really confuses me. What is the meaning and the point of having such a statement that declares the current module by the end of the file?
# Put in const.py...:
class _const:
class ConstError(TypeError): pass
def __setattr__(self,name,value):
if self.__dict__.has_key(name):
raise self.ConstError, "Can't rebind const(%s)"%name
self.__dict__[name]=value
import sys
sys.modules[__name__]=_const() #this I don't understand
# that's all -- now any client-code can
import const
Basically, my question is that in my opinion this line of code does not do anything; am I understanding it wrong?
Since in Python you don't have to put class definitions in separate files, I argue that I don't really need two modules unless I want to reuse the class "const." Then in this case sys.moldules[__name__]=_const()
is not necessary either... Am I understanding it correctly?