python之函数用法__str__()
2015-10-13 10:07
615 查看
# -*- coding: utf-8 -*- #python 27 #xiaodeng #python之函数用法__str__() #http://www.cnblogs.com/hongfei/p/3858256.html #__str__() #说明:直接打印对象的实现方法 #案例 class Fruit: '''Fruit类''' def __str__(self): # 定义对象的字符串表示 return self.__doc__ if __name__ == "__main__": fruit = Fruit() print str(fruit) #调用内置函数str()触发__str__()方法,输出结果为:Fruit类 print fruit #直接输出对象fruit,返回__str__()方法的值,输出结果为:Fruit类 print Fruit() #Fruit类,直接打印对象的实现方法 print Fruit #__main__.Fruit
相关文章推荐
- python 安装pymssql
- python 使用datetime模块timedelta实现日期时间相加
- python datetime模块用strftime 格式化时间
- python的程序格式要求
- Python 基础语法(四)
- Python 基础语法(三)
- python:IndentationError:expected an indented block错误解决
- python2.7 numpy scipy
- python tab键自动补齐命令
- 一键获取隐藏Wi-Fi SSID:利用Python和Scapy发现隐藏无线热点
- 在windows7(64位)下安装python(3.4)的theano库
- Python 数据库 sqlite3模块
- 详解Python中的元组与逻辑运算符
- Python中的字典与成员运算符初步探究
- 举例讲解Python中的身份运算符的使用方法
- Python的条件语句与运算符优先级详解
- 全面解析Python的While循环语句的使用方法
- 解析Python中while true的使用
- Eclipse + PyDev 调试 python代码
- Python打包(cx_Freeze)