1 _int_

定義在類中,創(chuàng)建類的實(shí)例的時(shí)候回先調(diào)用此方法,用于對(duì)該類的一些初始化(例如變量初始化)

2 _str_

    def _str_(self):
        return "this is a string'

  定義在類中,用print調(diào)用類的時(shí)候,會(huì)調(diào)用到此方法

class strtest:  
    def __str__(self):  
        return "str: this is only test"  
  
if __name__ == "__main__":  
    st=strtest()  
    print st  

 執(zhí)行結(jié)果為:

$./str.py

str: this is only test