欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

[Python]How to use magic methods in Python?

程序员文章站 2022-09-10 12:54:21
in this article, i write a simple code example to explore how to use super,new,init and st...

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