[Python]How to use magic methods in Python?

In this article, i write a simple code example to explore how to use super,new,init and str magic method in Python.

class MainClass(object):
    def __new__(cls,*args,**kwargs):        
        print 'this is the new method'
        return super(MainClass,cls).__new__(cls, *args,**kwargs)
        #return object.__new__(cls,*args,**kwargs)
    def __init__(self):
        print 'this is init method'    
    def __str__(self):
        return 'test the string method'    
def main():
    #help(object)
    instance = MainClass()
    print str(instance)
        
if __name__ == '__main__':    
    main()


發佈了270 篇原創文章 · 獲贊 3 · 訪問量 15萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章