首页 > 编程语言 > 详细

[Python]How to use magic methods in Python?

时间:2014-02-07 17:33:40      阅读:366      评论:0      收藏:0      [点我收藏+]

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()


[Python]How to use magic methods in Python?

原文:http://blog.csdn.net/wonderfan/article/details/18960431

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!