class RoundFloat(float):
def __new__(cls,val):
x=float.__new__(cls,round(val,2))
print x, type(x)
>>>RoundFloat(1.785)
1.78 <class '__main__.RoundFloat'>
What is the meaning of main here?
is the same meaning as in the if __name__="__main__"?
RoundFloatis an attribute of__main__(the file that is running) hence'__main__.RoundFloat'. If the class came from an imported module,__main__would be replaced by the module name.The purpose of
if __name__ == '__main__'is to check whether the module is the top-level script, and if it is, execute a block of code. If it was being imported, that block of code wouldn’t execute, since__name__would be the name of the module instead.