码迷,mamicode.com
首页 > 其他好文 > 详细

The str method

时间:2014-10-02 23:16:43      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   os   ar   for   sp   

__str__ is a special method name, like __init__, that is supposed to return a string representation of an object.

For example, here is a str method for Time objects. When you print an object, Python invokes the str method. when I write a new class, I almost always start by writing __init__, which makes it easier to instantiate objects, and __str__, which is almost always useful for debugging.

 def __str__(self):
        return (%.2d:%.2d:%.2d % (self.hour,self.minute,self.second))

bubuko.com,布布扣

 

from Thinking in Python

 

The str method

标签:style   blog   http   color   io   os   ar   for   sp   

原文地址:http://www.cnblogs.com/ryansunyu/p/4004402.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!