python Class:面向对象高级编程 __str__ / __repr__
2018-07-19 19:34
696 查看
其实:__str__ 与 __repr__效果一样, 人们说:__str__ 是面向用户的, 而__repr__是面向程序员的, 轰朵你?
官网解释:
Called by the
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
defines
used when an “informal” string representation of instances of that class is
required.
Called by the
differs from
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
![](https://oscdn.geek-share.com/Uploads/Images/Content/201912/08/e4b6739b43a9ea3ab2a2741942393ff7)
print Str('you')
![](https://oscdn.geek-share.com/Uploads/Images/Content/201912/08/97f206fbc7b2b13f69cd5d46d0ce310a)
print Repr('me')
![](https://oscdn.geek-share.com/Uploads/Images/Content/201912/08/fe8e62f5b8f7635b114bcc2bda0fb5e3)
最近非常的狂,不,不是最近,而是这个阶段的我,非常喜欢怼,如果我表达不好,那真是 抱歉了 。。。 你有理,你也来怼我啊,来啊,相互伤害啊!!!
官网解释:
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
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')
最近非常的狂,不,不是最近,而是这个阶段的我,非常喜欢怼,如果我表达不好,那真是 抱歉了 。。。 你有理,你也来怼我啊,来啊,相互伤害啊!!!
相关文章推荐
- python Class:面向对象高级编程 @property
- python Class:面向对象高级编程 __getattr__
- python Class:面向对象高级编程 @property
- python Class:面向对象高级编程 __call__& callable()
- python Class:面向对象高级编程 Enum(枚举)、@unique
- python Class:面向对象高级编程
- python Class:面向对象高级编程 元类:type
- python Class: 面向对象高级编程 __iter__ 和 next()
- python Class:面向对象高级编程 元类:metaclass
- python Class:面向对象高级编程 __getitem__
- python学习笔记(七) - 面向对象高级编程
- 廖雪峰Python教程 学习笔记11 面向对象高级编程
- Python学习--11 面向对象高级编程
- 【Python】学习笔记——-8.3、面向对象高级编程:3.多重继承
- 【廖雪峰Python习题集】面向对象高级编程
- python——(面向对象高级编程)使用@property
- python 面向对象高级编程
- Python3 基础:面向对象高级编程(上)
- python之面向对象高级编程
- Python面向对象高级编程——学习笔记