python内置函数__init__及__str__的区别
来源:互联网 发布:网络信息安全应急演练 编辑:程序博客网 时间:2024/06/08 13:35
原址
一、说明:
__init__:类实例初始化函数
__str__:类实例字符串化函数
二、
示例1:
1
2
3
4
5
6
class
Friend():
def
__init__(
self
, name):
self
.name
=
name
if
__name__
=
=
'__main__'
:
friend
=
Friend(
'Liang'
)
print
friend
输出结果:<__main__.Friend instance at 0x1d92a70>
上面的返回结果是存储地址, 如何打印出一个对象的有感内容呢? 可以通过__str__函数
实例2:
1
2
3
4
5
6
7
8
class
Friend():
def
__init__(
self
, name):
self
.name
=
name
def
__str__(
self
):
return
"Freind : %s"
%
self
.name
if
__name__
=
=
'__main__'
:
friend
=
Friend(
'Liang'
)
print
friend
输出结果: Friend: Liang
阅读全文
0 0
- python内置函数__init__及__str__的区别
- python内置函数__init__及__str__的区别
- Python中class的内置函数__str__
- Python中class的内置函数__str__
- Python中class的内置函数__str__
- Python -- class内置函数__str__
- python内置函数__str__作用说明
- python 内置__str__作用
- 理解python中的__str__, __call__, __init__, __getattr__
- python __repr__和__str__ 的区别
- python中__str__和__repr__的区别
- python中__str__()函数的用法
- python __str__ , __repr__区别
- python __init__和__new__之间的区别
- python __init__和__new__之间的区别
- python中__new__和__init__的区别
- python __init__和__new__之间的区别
- Python中的__new__和__init__的区别
- SpringMVC学习笔记----
- 把 if-else 的代码风格改成表格驱动法的意义
- 关于搭建数据库的一些问题
- Docker 镜像优化与最佳实践
- 给窗体中控件绘图的几种方法
- python内置函数__init__及__str__的区别
- 输入一个数,输出其二进制数列。
- Picnic Planning POJ
- 谈内核资源申请platform_get_resource、devm_request_mem_region、devm_ioremap
- Yii2.0-advanced-11—restfull架构
- 我的Laravel学习之路之环境配置
- 推流工具
- checkbox的全选
- QNX系统的ftp调试,连接FTP调试-QNX的PC104调试ftp