【http://blog.csdn.net/followingturing/article/details/7954204】 python
__str__ 直接打印對象的實現方法 ssh
在python語言裏,__str__通常是格式是這樣的。
class A:
def __str__(self):
return "this is in str"
事實上,__str__是被print函數調用的,通常都是return一個什麼東西。這個東西應該是以字符串的形式表現的。若是不是要用str()函數轉換。當你打印一個類的時候,那麼print首先調用的就是類裏面的定義的__str__,好比:str.py 函數
#!/usr/bin/env python class strtest: def __init__(self): print "init: this is only test" def __str__(self): return "str: this is only test" if __name__ == "__main__": st=strtest() print st
$./str.py init: this is only test str: this is only test從上面例子能夠看出,當打印strtest的一個實例st的時候,__str__函數被調用到。
>>> dir({}) ['__class__', '__cmp__', '__contains__', '__delattr__', '__delitem__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'has_key', 'items', 'iteritems', 'iterkeys', 'itervalues', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values', 'viewitems', 'viewkeys', 'viewvalues'] >>> t={} >>> t['1'] = "hello" >>> t['2'] = "world" >>> t {'1': 'hello', '2': 'world'} >>> print t {'1': 'hello', '2': 'world'} >>> t.__str__() "{'1': 'hello', '2': 'world'}" >>>你們能夠看到一個字典,print t 和 t.__str__()是同樣的。只不過__str__()將字典內容以字符串形式輸出。
#!/us/bin/env/python #__metaclass__ = type #if __name__ == "__main__": class strtest: def __init__(self): self.val = 1 def __str__(self): return self.val if __name__ == "__main__": st=strtest() print st$./str1.py
#!/usr/bin/env python #__metaclass__ = type #if __name__ == "__main__": class strtest: def __init__(self): self.val = 1 def __str__(self): return str(self.val) if __name__ == "__main__": st=strtest() print st$./str2.py
【http://maxomnis.iteye.com/blog/1911208】 ui
Python 有辦法將任意值轉爲字符串:將它傳入repr() 或str() 函數。
函數str() 用於將值轉化爲適於人閱讀的形式,而repr() 轉化爲供解釋器讀取的形式
(若是沒有等價的語法,則會發生SyntaxError 異常) 某對象沒有適於人閱讀的解釋形式的話, str() 會返回與repr()等同的值。不少類型,諸如數值或鏈表、字典這樣的結構,針對各函數都有着統一的解讀方式。
字符串和浮點數,有着獨特的解讀方式。
this
>>> class Person(object): def __str__(self): return "in__str__" def __repr__(self): return "in_repr__" >>> p = Person() >>> p in_repr__ >>> str(p) 'in__str__' >>> repr(p) 'in_repr__' >>> print repr(p) in_repr__ >>> print p in__str__ p跟print repr(p)和print p一致。
str()通常是將數值轉成字符串。
repr()是將一個對象轉成字符串顯示,注意只是顯示用,有些對象轉成字符串沒有直接的意思。如list,dict使用str()是無效的,但使用repr能夠,這是爲了看它們都有哪些值,爲了顯示之用。
The str() function is meant to return representations of values which are fairly
human-readable, while repr() is meant to generate representations which can be read by
the interpreter (or will force a SyntaxError if there is not equivalent syntax). For
objects which don't have a particular representation for human consumption, str() will
return the same value as repr(). Many values, such as numbers or structures like lists
and dictionaries, have the same representation using either function. Strings and
floating point numbers, in particular, have two distinct representations.
.net
>>> s = "Hello, world." >>> str(s) 'Hello, world.' >>> repr(s) "'Hello, world.'" >>> str(0.1) '0.1' >>> repr(0.1) '0.1' >>> x = 10 * 3.25 >>> y = 200 * 200 >>> s = 'The value of x is '+ repr(x) + ', and y is ' + repr(y) + '...' >>> print s The value of x is 32.5, and y is 40000... >>> hello = 'hello, world\n' >>> hellos = repr(hello) >>> print hellos 'hello, world\n' >>> hellos "'hello, world\\n'" >>>