Python 特殊方法

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python 特殊方法相关的知识,希望对你有一定的参考价值。

 

 

1 _int_

定义在类中,创建类的实例的时候回先调用此方法,用于对该类的一些初始化(例如变量初始化)

2 _str_

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

  定义在类中,用print调用类的时候,会调用到此方法

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

 执行结果为:

$./str.py

str: this is only test

以上是关于Python 特殊方法的主要内容,如果未能解决你的问题,请参考以下文章

Python中verbaim标签使用详解

jquery中的$的特殊用法

Python学习 :函数

#5 Python面向对象

python 有用的Python代码片段

Python 向 Postman 请求代码片段