您好,登錄后才能下訂單哦!
其實:__str__ 與 __repr__效果一樣, 人們說:__str__ 是面向用戶的, 而__repr__是面向程序員的, 轟朵你?
官網解釋:
object.
__repr__
(self)?
Called by the repr()
built-in function and by string conversions (reverse
quotes) to compute the “official” string representation of an object. If at all
possible, this should look like a valid Python expression that could be used to
recreate an object with the same value (given an appropriate environment). If
this is not possible, a string of the form <...some useful description...>
should be returned. The return value must be a string object. If a class
defines __repr__()
but not __str__()
, then __repr__()
is also
used when an “informal” string representation of instances of that class is
required.
object.
__str__
(self)?
Called by the str()
built-in function and by the print
statement to compute the “informal” string representation of an object. This
differs from __repr__
()
in that it does not have to be a valid Python
expression: a more convenient or concise representation may be used instead.
The return value must be a string object.
看了解釋,哪里說是面向用戶,哪里又說面向程序員了????看到加粗字了沒有,看到下滑線了沒有???這里說的是作用于所有人好嘛!!!如果非要以自己的職位來說,好吧,好像是這么一回事,真是 淫穢 隱晦的表達。
不說了,先小試一下:
#!/usr/bin/python
# -*- coding: utf-8 -*-
class Student(object):
def __init__(self, name):
self.hah = name
class Str(Student):
def __str__(self):
return 'Str (name: %s)' %self.hah
class Repr(Student):
def __repr__(self):
return 'Repr(name: %s)' % self.hah
s = Student('Michael')
print s.hah
print Str('you')
print Repr('me')
最近非常的狂,不,不是最近,而是這個階段的我,非常喜歡懟,如果我表達不好,那真是 抱歉了 。。。 你有理,你也來懟我啊,來啊,相互傷害啊!!!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。